Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Opencv-Python Script (Error Message) When I run the script below, I get the foll

ID: 639746 • Letter: O

Question

Opencv-Python Script (Error Message)

When I run the script below, I get the following error message:

ERROR: Missing Arguments

HOW: python C:/Python27/assignment_01.py image.jpg name last_name

Traceback (most recent call last):

File "C:/Python27/assignment_01.py", line 8, in

sys.exit(1)

SystemExit: 1

THE ERROR IS OCCURRING IN THE LINE: print " HOW: ... I CANNOT UNDERSTAND WHY I'M GETTING AN ERROR. ANY HELP WOULD BE MUCH APPRECIATED. I'M USING THE PYTHON SHELL IDLE. THX!

_____________________________________________________-

import sys
import cv2

#Check and Read arguments
if len(sys.argv) < 4:
    print "ERROR: Missing Arguments"
    print " HOW: python %s image.jpg name last_name" % (sys.argv[0])
    sys.exit(1)


    input_image = sys.argv[1]
    name = sys.argv[2]
    last_name = sys.argv[3]

    #Read image
    img = cv2.imread(sys.argv[1],1)

    #Write image
    str_to_write = name + " " + last_name
    font = cv2.FONT_HERSHEY_SIMPLEX
    color = (0,0,255) #Red because OpenCV is BGR
    rows,cols,depth = img.shape

    #cv2.putText(img,str_to_write,(rows/10,cols/10),font,2,color,6,cv2.LINE_AA)
    cv2.putText(img,str_to_write,(rows/10,cols/10),font,2,color,6,cv2.CV_AA)

    #Show image and wait
    cv2.imshow('image',img)
    k = cv2.waitKey(0)

    #wait for ESC key to exit
    if k == 27:
        cv2.destroyAllWindows()

    #wait for 's' key to save and exit
    elif k == ord('s'):
        output_file_name = last_name + "_output.jpg"
        cv2.imwrite(output_file_name,img)
        cv2.destroyAllWindows()

Explanation / Answer

The mentioned script is comand line script you need to have jpg image in the same folder in order to run it.

Command Usage:

python imageproc.py graphicoutput.jpg Sumita Gupta

here,

imageproc.py = your python script

graphicoutput.jpg = Sample JPG image file in the same folder where script is placed.

Sumita = First Name

Gupta = Last Name

Post script execution Gupta_output.jpg image file is created in the same location.