Skip to content

Instantly share code, notes, and snippets.

@abdul-rehman-2050
Created March 7, 2018 22:04
Show Gist options
  • Save abdul-rehman-2050/2841990209878d219deab62787952986 to your computer and use it in GitHub Desktop.
Save abdul-rehman-2050/2841990209878d219deab62787952986 to your computer and use it in GitHub Desktop.
Face detector using Opencv Python version 2.4 and frontal face cascade features
import cv2
import sys
video_capture = cv2.VideoCapture(0)
#video_capture.set(3,320)
#video_capture.set(4,240)
face_cascade = cv2.CascadeClassifier('haar_frontalface_alt2.xml')
if __name__ == "__main__":
try:
while(True):
# Capture frame-by-frame
ret, frame = video_capture.read()
if ret==True:
#just to remove mirror effect in camera
frame = cv2.flip(frame,2)
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_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), thickness=cv2.cv.CV_FILLED)
cv2.rectangle(frame, (x, y), (x+w+3, y+h+3), (0, 0, 0), 2)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
except KeyboardInterrupt as k:
sys.stderr.write("program will exit\nBye!\n")
except Exception, e:
sys.stderr.write(str(e) + "\n")
# When everything done, release the capture
video_capture.release()
cv2.destroyAllWindows()
sys.exit(0)
@vardaan248
Copy link

module 'cv2.cv2' has no attribute 'cv'
Traceback (most recent call last):
File "C:\Users\VARDAAN\Desktop\Project\create_data.py", line 42, in
cv2.destroyAllWindows()
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:621: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvDestroyAllWindows'

@abdul-rehman-2050
Copy link
Author

The issue is related to your OpenCV version. The code is compiled with OpenCV 2.4 i guess

@GearKite
Copy link

GearKite commented Jul 29, 2020

I am having this issue [ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (436) 'anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

Copy link

ghost commented Dec 30, 2020

I am having this issue [ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (436) 'anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

use CAP_DSHOW on windows

@parkdongju2
Copy link

I am having this issue [ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (436) 'anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

use CAP_DSHOW on windows

how to use CAP_DSHOW?

Copy link

ghost commented Mar 28, 2021

cv2.VideoCapture(0, cv2.CAP_DSHOW)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment