Skip to content

Instantly share code, notes, and snippets.

@kshwetabh
Last active August 14, 2019 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshwetabh/ec031fcbb33d73d6ad6833cc75377f43 to your computer and use it in GitHub Desktop.
Save kshwetabh/ec031fcbb33d73d6ad6833cc75377f43 to your computer and use it in GitHub Desktop.
This script requires IPCam app (or any other app that can stream images/videos over HTTP) to be installed on Android device and OpenCV to installed on Desktop machine. On running this script, OpenCV will detect front-faces in videos streamed on the desktop. #python #opencv #android #face-detection
import requests
import cv2
import numpy as np
# Online code reference: https://www.youtube.com/watch?v=-mJXEzSD1Ic
# Image URL being streamed from IPWebCam app (I used the one written by Pavel Khlebovich).
# Make sure that the android device and the machine running opencv are on the same network and
# you can access the streaming URL from that machine
url = 'http://192.168.43.236:8080/shot.jpg'
# HAARCascade Classifier for Frontal faces
face_cascade = cv2.CascadeClassifier("data\haarcascade_frontalface_default.xml")
# Run infinite loop (unless terminated by pressing Esc key on OpenCV video window)
while True:
# Get image from the IPWeb Cam
img_resp = requests.get(url)
img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
img = cv2.imdecode(img_arr, -1)
# Reduce the size of the image to half to increase performance of recognition algorithm
# (it will have to interpret less pixels / data
resize = cv2.resize(img, (int(img.shape[1]/2),int(img.shape[0]/2)))
# There might be multiple faces in the video/image streamed. Play around with
# scaleFactor & minNeighbors to get a satisfactory recognition
faces = face_cascade.detectMultiScale(resize, scaleFactor=1.03, minNeighbors=5)
# Draw rectangle boxes over the identified faces
for x,y,w,h in faces:
resize = cv2.rectangle(resize, (x,y), (x+w, y+h), (0, 255, 0), 3)
# Show stream with rectangle drawn over faces in opencv window
cv2.imshow("AndroidCam", resize)
# continue detection until user hits ESC button
if cv2.waitKey(1) == 27:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment