Search code examples
pythonopencvcaffe

Window does not pop up for cv2 and caffe


I am attempting to load a youtube video, put a box over the face and predict their ages and gender. However, when i run the code, it just executes but the pop up window does not appear. My code is below. I'm not sure where the problem is. Thanks for your help guys

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()

Running this does pull out the window though

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()

Solution

  • You have creatde two functions video_detector() and init_caffe_models() which have never been called. Your code is just reaching till gender_list = ["Male","Female"] and then exiting without ever entering those two functions.