Q:

Python input gives error in user key in name

I have a code as follow.
The code has error at line name = input("Enter User Name: "). It doesn't accept the user key in, what could be wrong?
The error is 
Enter User Name: test
Traceback (most recent call last):
  File "demos/TrainReg/PrepareFaces.py", line 48, in click_and_crop
    name = input("Enter User Name: ")
  File "<string>", line 1, in <module>
NameError: name 'test' is not defined
Traceback (most recent call last):
  File "demos/TrainReg/PrepareFaces.py", line 80, in <module>
    cv2.imwrite(rawFolder+'/'+name_+"/image-"+str(count)+".jpg",crop);
TypeError: cannot concatenate 'str' and 'NoneType' objects

The code is 
import numpy as np
np.set_printoptions(precision=2)
import os
import cv2

ix = 0
iy = 0
boxCX = 0
boxCY = 0
boxW = 0
boxH = 0
name_=None
click = False
completed_rect = False
boxRefPt = []
cwd = os.getcwd()
rawFolder=os.path.join(cwd, 'data/mydataset/raw')

def click_and_crop(event, x, y, flags, param):
    global name_, boxRefPt, boxCX, boxCY, boxW, boxH, click, ix, iy, completed_rect

    if event == cv2.EVENT_LBUTTONDOWN:              # check if the left mouse button is clicked.
        if completed_rect == False:                 # check if no rectangle is drawn.
            boxRefPt = [(x, y)]                     # record the starting (x, y) coordinate of a new rectangle.
            ix, iy = x, y                           # record mouse movements (ix, iy) coordinate.
            click = True                            # a valid left mouse click (hold) is detected.

    elif event == cv2.EVENT_RBUTTONDOWN:            # check if right mouse if clicked.
        boxCX = 0
        boxCY = 0
        boxW = 0
        boxH = 0
        completed_rect = False

    elif event == cv2.EVENT_LBUTTONUP:              # check if the left mouse button is released.
        boxRefPt.append((x, y))                     # record the ending (x, y) coordinate a new rectangle.
        click = False                               # no left mouse click is detected.
        completed_rect = True
        (x1, y1) = boxRefPt[0]
        (x2, y2) = boxRefPt[1]

        boxCX = min(x1, x2) 
        boxCY = min(y1, y2)
        boxW = abs(x1 - x2)
        boxH = abs(y1 - y2)

        name = input("Enter User Name: ")

        name_ = name
        #create folder
        os.mkdir(rawFolder+'/'+name_) 

    elif event == cv2.EVENT_MOUSEMOVE:              # check if the mouse is moving.
        if click == True:                           # check if the left mouse button is clicked.
            ix, iy = x, y                           # record mouse movements (ix, iy) coordinate.

source=-1
cap = cv2.VideoCapture(source)
if cap is None or not cap.isOpened():
   print('Warning: unable to open video source: ', source)
   exit()

cv2.namedWindow("image")
cv2.setMouseCallback("image", click_and_crop)

count=0
while(True):    
    #Capture frame-by-frame
    ret, frame = cap.read()
    #crop image
    if completed_rect == True:
       cv2.rectangle(frame, boxRefPt[0], boxRefPt[1], (0, 255, 0), 2)
       cv2.putText(frame,'LEARN AREA'+str(count+1), (boxCX, (boxCY - 12)), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), lineType=cv2.LINE_AA)
       cv2.putText(frame, name_, (boxCX, (boxCY + boxH + 24)), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), lineType=cv2.LINE_AA)
       crop=frame[boxCY+2:boxCY+boxH-2,boxCX+2:boxCX+boxW-2].copy()
       count=count+1
       cv2.imwrite(rawFolder+'/'+name_+"/image-"+str(count)+".jpg",crop);
       #cv2.waitKey(1000)
    if click == True and completed_rect == False:
       cv2.rectangle(frame, boxRefPt[0], (ix, iy), (0, 255, 0), 2) 
    cv2.imshow("image", frame)
    k = cv2.waitKey(10) & 0xFF
    if k == ord('q'):
       break
cap.release()
cv2.destroyAllWindows()    

A:

In Python 2, you should use raw_input instead of input. In Python 3, use input. The error you see is from Python 2 input function. See reference for Python 2 input here

