0% found this document useful (0 votes)
22 views1 page

Detection

The document shows code for face detection using OpenCV. It imports OpenCV, defines camera and display parameters, creates trackbars to control detection settings, loads a face classifier, and runs a main loop to capture video, detect faces, and display the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views1 page

Detection

The document shows code for face detection using OpenCV. It imports OpenCV, defines camera and display parameters, creates trackbars to control detection settings, loads a face classifier, and runs a main loop to capture video, detect faces, and display the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import cv2

################################################################
path = 'haarcascades/haarcascade_frontalface_default.xml' # PATH OF THE CASCADE
cameraNo = 1 # CAMERA NUMBER
objectName = 'Arduino' # OBJECT NAME TO DISPLAY
frameWidth= 640 # DISPLAY WIDTH
frameHeight = 480 # DISPLAY HEIGHT
color= (255,0,255)
#################################################################

cap = cv2.VideoCapture(cameraNo)
cap.set(3, frameWidth)
cap.set(4, frameHeight)

def empty(a):
pass

# CREATE TRACKBAR
cv2.namedWindow("Result")
cv2.resizeWindow("Result",frameWidth,frameHeight+100)
cv2.createTrackbar("Scale","Result",400,1000,empty)
cv2.createTrackbar("Neig","Result",8,50,empty)
cv2.createTrackbar("Min Area","Result",0,100000,empty)
cv2.createTrackbar("Brightness","Result",180,255,empty)

# LOAD THE CLASSIFIERS DOWNLOADED


cascade = cv2.CascadeClassifier(path)

while True:
# SET CAMERA BRIGHTNESS FROM TRACKBAR VALUE
cameraBrightness = cv2.getTrackbarPos("Brightness", "Result")
cap.set(10, cameraBrightness)
# GET CAMERA IMAGE AND CONVERT TO GRAYSCALE
success, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# DETECT THE OBJECT USING THE CASCADE
scaleVal =1 + (cv2.getTrackbarPos("Scale", "Result") /1000)
neig=cv2.getTrackbarPos("Neig", "Result")
objects = cascade.detectMultiScale(gray,scaleVal, neig)
# DISPLAY THE DETECTED OBJECTS
for (x,y,w,h) in objects:
area = w*h
minArea = cv2.getTrackbarPos("Min Area", "Result")
if area >minArea:
cv2.rectangle(img,(x,y),(x+w,y+h),color,3)
cv2.putText(img,objectName,(x,y-
5),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,color,2)
roi_color = img[y:y+h, x:x+w]

cv2.imshow("Result", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

You might also like