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

after running the code in python the image of my webcam turn into this

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
  • 2
    can you reduce your code to find out whether distortionnis already present directly after capturing or color conversion? – Micka Aug 13 '20 at 06:04
  • i try with other simple code but the webcam stream is the same. – shazni 11 Aug 13 '20 at 07:55
  • 1
    can you post the minimal code that already leads to the problem? – Micka Aug 13 '20 at 08:10
  • the code works find before this, could it be some library that i installed and other software cause this problem, i installed Cmake and other library dlib, opencv face recogntion, opencv contrib python and other – shazni 11 Aug 14 '20 at 02:08
  • I had the same problem and this solution, although it is for a different problem, worked for me `video_capture = cv2.VideoCapture(cv2.CAP_DSHOW)` Taken from this stackoverflow answer [link](https://stackoverflow.com/a/53405720/1040724) – ICTAddict Sep 30 '21 at 17:27

2 Answers2

0

Your code works for me without any problems.
The output image looks like there is a problem with the data type of the input image.

Something similar was discussed here:
https://answers.opencv.org/question/174027/c-ycbcr422-to-rgb-convert-from-raw-data-file/

Can you please also post your input frame grayscale image?
What camera are you using?

try add this code after video_capture.read() and post output.

print(frame.dtype)
print(frame.shape)

Mine is: dtype - uint8, shape - (480, 640, 3).
You can have a problem with the number of channels (for example RGBA) and rectangle function.

These are just my ideas of what could be wrong. But for me, your code works fine.

br Jozef

  • uint8 (480, 640, 3), the code actually work find before this. could it be other library or software that i installed. I try to test my webcam and it works find – shazni 11 Aug 14 '20 at 02:10
0

USB compatibility should be 1.1. If you are using vmware

enter image description here