Skip to main content

Face detection in Python OpenCV using smartphone Camera

Prerequisites

  • Text Editor ( Anyone IDE PyCharm, Visual studio code, etc.,)
  • Installed Python >=3.6
  • Install opencv-python package (reference)
  • Webcam or Smart phone (phone should installed DroidCam Webcam - here Playstore link)

Code

#Sample created by chandran marimuthu on Mar-2022
import cv2
#trained datasets link: https://github.com/opencv/opencv/tree/master/data/haarcascades
trainedDataset=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

##Default camera
#video=cv2.VideoCapture(0)

##Local video files
#video=cv2.VideoCapture('videos/chn_video.mp4')

#Web cam link
video=cv2.VideoCapture('http://192.168.43.157:4747/video?640x480')
while True:
success, frame = video.read()
if success == True:
##To get gray scale image
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = trainedDataset.detectMultiScale(frame)
##Printing detected face range
#print(faces)
for x, y, w, h in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
##Circle face
#cv2.circle(frame, (x + w//2, y + h//2), 100, (0, 0, 255), 2)
#video is Title of the screen
cv2.imshow('Sample face detection', frame)
cv2.waitKey(1)
else:
print('No frame present!')
break


Output







Comments