Ask Your Question

Revision history [back]

Webcam generated image distorted and python stop working

import cv2 import sys

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

video_capture = cv2.VideoCapture(0)

img_counter = 0

while True: # Capture frame-by-frame ret, frame = video_capture.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
k = cv2.waitKey(1)
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.5,
    minNeighbors=5,
    minSize=(30, 30),
    flags=cv2.CASCADE_SCALE_IMAGE
)

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the resulting frame
cv2.imshow('FaceDetection', frame)

if k%256 == 27: #ESC Pressed
    break
elif k%256 == 32:
    # SPACE pressed
    img_name = "facedetect_webcam_{}.png".format(img_counter)
    cv2.imwrite(img_name, frame)
    print("{} written!".format(img_name))
    img_counter += 1

When everything is done, release the capture

video_capture.release() cv2.destroyAllWindows()

running the code works fine, but after i try to installed CMake and dlib library which i failed to do the image of the webcam is distorted and python stop working

this is the image generated from the webcam

Webcam generated image distorted and python stop working

Code:

import cv2
import sys

sys faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cv2.CascadeClassifier('haarcascade_frontalface_default.xml') video_capture = cv2.VideoCapture(0)

cv2.VideoCapture(0) img_counter = 0

0 while True: # Capture frame-by-frame ret, frame = video_capture.read()

video_capture.read()


    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 k = cv2.waitKey(1)
 faces = faceCascade.detectMultiScale(
     gray,
     scaleFactor=1.5,
     minNeighbors=5,
     minSize=(30, 30),
     flags=cv2.CASCADE_SCALE_IMAGE
 )

 # Draw a rectangle around the faces
 for (x, y, w, h) in faces:
     cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

 # Display the resulting frame
 cv2.imshow('FaceDetection', frame)

 if k%256 == 27: #ESC Pressed
     break
 elif k%256 == 32:
     # SPACE pressed
     img_name = "facedetect_webcam_{}.png".format(img_counter)
     cv2.imwrite(img_name, frame)
     print("{} written!".format(img_name))
     img_counter += 1

# When everything is done, release the capture

capture video_capture.release() cv2.destroyAllWindows()

cv2.destroyAllWindows()

running the code works fine, but after i try to installed CMake and dlib library which i failed to do the image of the webcam is distorted and python stop working

this is the image generated from the webcam