PacktPublishing / Mastering-Computer-Vision-with-TensorFlow-2.0

Mastering Computer Vision with TensorFlow 2.0, published by Packt
MIT License
91 stars 68 forks source link

Chapter3_face keypoint_detection: The script is fixed at the cv2.namedWindow("test") #12

Open mikechen66 opened 3 years ago

mikechen66 commented 3 years ago

I tried the script again after installing dlib and related libraries. I can see my face with executing the the 1st section of the code. However, the script could not execute the rest of the lines of the code. So it is quite strange.

1. Only exectue the lines of code

I can see the camera works correctly and show my face. But camera screen is keep shaking.

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
....

face_cascade = cv2.CascadeClassifier('./cv2/data/haarcascade_frontalface_default.xml')

img_size = 299

# -cam = cv2.VideoCapture(0)
cam = cv2.VideoCapture(-1)

cv2.namedWindow("test")

2. Do not execute the rest of the line of the code.

The rest of the lines of code are not executed. It is quite strange.

img_counter = 0

while(True):
    ret, frame = cam.read()
    faces = face_cascade.detectMultiScale(frame, 1.3, 5)
    for (x,y,w,h) in faces:
        if w >130:
            detected_face = frame[int(y):int(y+h), int(x):int(x+w)] 
            cv2.imshow("test", detected_face)
    if not ret:
        break
    k = cv2.waitKey(1)

    if k%256 == 27:
        # ESC pressed
        print("hit Esc to skipe and hit Space bar to save image")
        break
    elif k%256 == 32:
        # SPACE pressed 
        faceresize = cv2.resize(detected_face, (img_size,img_size)) 
        img_name = "dataset/opencv_frame_{}.jpg".format(img_counter)
        cv2.imwrite(img_name, faceresize)
        print("{} written!".format(img_name))
        img_counter += 1

cam.release()
........

3. Can not exit from the camera

The camera is keeping moving and working. After closing the script with jupyter notebook, I still can not exit from the camera. The camera is showing my face until turning off the computer.

gaurav-packt commented 3 years ago

Hello @mikechen66,

Can you please share relevant order details as to from where did you purchased the book so that we can assist you?

Thanks