Window 不弹出cv2和caffe

Window does not pop up for cv2 and caffe

我正在尝试加载一个 youtube 视频,在脸上放一个方框并预测他们的年龄和性别。但是,当我 运行 代码时,它只是执行但弹出 window 不会出现。我的代码如下。我不确定问题出在哪里。感谢你们的帮助

import pafy
import cv2

url = 'https://www.youtube.com/watch?v=dTL7LriRZJQ'    #URL that you want to copy
vPafy= pafy.new(url) #get URL and get best mp4 video 
play = vPafy.getbest(preftype="mp4")

#Video capture
cap = cv2.VideoCapture(play.url) 

MODEL_MEAN_VALUES = (78.4, 87.7, 114.9)
age_list = ['(0, 2)', '(4, 6)', '(8, 12)', '(15, 20)', '(25, 32)', '(38, 43)', '(48, 53)', '(60, 100)']
gender_list = ['Male', 'Female']

# defining a function to load prototxt & caffe 
def init_caffe_models():
    age_net = cv2.dnn.readNetFromCaffe('/Users/Desktop/Gender-and-Age-Detection-master/age_deploy.prototxt', '/Users/Desktop/Gender-and-Age-Detection-master/age_net.caffemodel')
    gender_net = cv2.dnn.readNetFromCaffe('/Users/Desktop/Gender-and-Age-Detection-master/gender_deploy.prototxt','/Users/Desktop/Gender-and-Age-Detection-master/gender_net.caffemodel')
    return(age_net, gender_net)

# defining a function for capturing the video
def video_detector(age_net, gender_net):
    font = cv2.FONT_HERSHEY_SIMPLE

    while True:
        ret, frame = cap.read()     #returns a boolean    
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        face_cascade = cv2.CascadeClassifier('/Users/Desktop/OpenCV-Python-Series-master/src/cascades/data/haarcascade_frontalface_default.xml')
        faces = face_cascade.detectMultiScale(gray, 1.1, 5) # input = grey image, 1.1 = scale factor


    for (x, y, w, h )in faces: #looping through faces and drawing a rectangle with cv2.rectangle 
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 0), 2)
        face_img = image[y:y+h, x:x+w].copy()    
        blob = cv2.dnn.blobFromImage(face_img, 1, (227, 227), MODEL_MEAN_VALUES, swapRB=False)

        #Predict Gender
        gender_net.setInput(blob)
        gender_preds = gender_net.forward()
        gender = gender_list[gender_preds[0].argmax()]
        print("Gender : " + gender)

        #Predict Age
        age_net.setInput(blob)
        age_preds = age_net.forward()
        age = age_list[age_preds[0].argmax()]
        print("Age Range: " + age)

        overlay_text = "%s %s" % (gender, age)
        cv2.putText(frame, overlay_text, (x, y), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
        cv2.imshow('frame', frame)  
#0xFF is a hexadecimal constant which is 11111111 in binary.
        if cv2.waitKey(20) & 0xFF == ord('q'): 
            break
    cap.release()
    cv2.destroyAllWindows()

运行 虽然

这确实拉出了 window
import pafy
import cv2

# get api key, and dont name the file pafy/Pafy
url = 'https://www.youtube.com/watch?v=dTL7LriRZJQ'    #URL that you want to copy
vPafy= pafy.new(url,basic=True) #get URL and get best mp4 video 
play = vPafy.getbest(preftype="mp4")

cap = cv2.VideoCapture(play.url) #cap = cv2.VideoCapture(0) only for webcam
cap.set(3, 480) #set width of the frame
cap.set(4, 640) #set height of the frame 

while True:
    ret, image = cap.read()  
    cv2.imshow('frame',image)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break    

cap.release()
cv2.destroyAllWindows()

您创建了两个从未调用过的函数 video_detector() 和 init_caffe_models()。您的代码只是到达 gender_list = ["Male","Female"] 然后退出而没有输入这两个函数。