代码之家  ›  专栏  ›  技术社区  ›  batuman

Python输入在名称中的用户密钥中出现错误

  •  0
  • batuman  · 技术社区  · 7 年前

    我有一个代码如下。

    代码在第行有错误 name = input("Enter User Name: ") . 它不接受用户输入,有什么问题吗?

    错误是

    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
    

    代码是

    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()    
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Hai Vu    7 年前

    在Python2中,应该使用 raw_input 而不是 input . 在Python3中,使用 . 您看到的错误来自Python 2 输入 功能。请参阅Python2的参考资料 输入 here