starting cv2 and face recognition.
import cv2
cap=cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
while True:
ret,frame=cap.read()
gray_frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
if ret == False:
continue
faces=face_cascade.detectMultiScale(gray_frame,1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow("VideoFrame",frame)
key_pressed = cv2.waitKey(1) & 0xFF
if key_pressed == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
******************************designing opencv logo****************************
import cv2
import numpy as np
#angles are measured clockwise
img_=np.zeros((591,591,3))
c_c=(300,180)
axes_len=(80,80)
angle = 45
startAngle = 90
endAngle = 360
color = (255,0,0)
thickness = 10
cv2.ellipse(img_,c_c,axes_len,angle,startAngle,endAngle,color,thickness)
cv2.ellipse(img_,(180,361),axes_len,50,0,270,(0,255,0),10)
cv2.ellipse(img_,(420,361),axes_len,145,90,360,(0,0,255),10)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img_,'OpenCV',(50,550), font, 4,(255,255,255),2,cv2.LINE_AA)
cv2.imshow('image',img_)
cv2.waitKey()
cv2.destroyAllWindows()
**********************************eyes_classifier**********************
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('my_photo.jpg')
fix_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
eye_classifier = cv2.CascadeClassifier('haarcascade_eyes.xml')
def detect_eyes(fix_img):
eyes_rect = eye_classifier.detectMultiScale(fix_img)
for (x,y,w,h) in eyes_rect:
cv2.rectangle(fix_img,(x,y),(x+w,y+h),(0,0,0),4)
return fix_img
result = detect_eyes(fix_img)
plt.imshow(result)
Problem with detecting face
ReplyDelete