Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # croppfacedetection.py
- #Author: Waheed Rafiq
- #Research Student Birmingham City University
- #Date: 03/11/2016
- #Description : Save capture face in a folder.
- #Import library required for Capture face.
- # Should you wish to use this code for
- #education purpose in your assignment or dissertation
- # please use the correct citation and give credit where required.
- from watson_developer_cloud import VisualRecognitionV3
- visual_recognition = VisualRecognitionV3(
- '2018-03-19',
- api_key='d3b2d058a14810b07314d231f2f84c26bb305c8d')
- import json
- import cv2
- import threading
- size = 4
- webcam = cv2.VideoCapture(0) #Use camera 0
- # We load the xml file
- classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
- # Above line normalTest
- #classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
- #Above line test with different calulation
- #classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt_tree.xml')
- #classifier = cv2.CascadeClassifier('lbpcascade_frontalface.xml')
- watsonDone=True
- def watsonDoYourJob():
- print("watson do you job")
- try:
- with open('lastHead.jpg', 'rb') as images_file:
- faces = visual_recognition.detect_faces(images_file)
- print(json.dumps(faces, indent=2))
- if __name__ == '__main__':
- data = json.dumps(faces, indent=2)
- data = json.loads(data)
- minAge = data["images"][0]["faces"][0]
- print(minAge)
- # cv2.putText(image, "Hello World!!!", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
- except Exception as e:
- print(e)
- global watsonDone
- watsonDone=True
- while True:
- (rval, im) = webcam.read()
- im=cv2.flip(im,1,0) #Flip to act as a mirror
- # Resize the image to speed up detection
- mini = cv2.resize(im, (round(im.shape[1] / size), round(im.shape[0] / size)))
- # detect MultiScale / faces
- faces = classifier.detectMultiScale(mini)
- # Draw rectangles around each face
- for f in faces:
- (x, y, w, h) = [v * size for v in f] #Scale the shapesize backup
- cv2.rectangle(im, (x, y), (x + w, y + h),(0,255,0),thickness=4)
- #Save just the rectangle faces in SubRecFaces
- startX=x-30
- if(startX)<0:
- startX=0
- startY = y - 30
- if (startY) < 0:
- startY = 0
- endX=startX+300
- endY = startY + 300
- sub_face = im[startY:endY, startX:endX]
- FaceFileName = "lastHead.jpg"
- cv2.imwrite(FaceFileName, sub_face)
- if(watsonDone):
- watsonDone=False
- threading._start_new_thread(watsonDoYourJob,())
- # Show the image
- cv2.imshow('your face', im)
- key = cv2.waitKey(10)
- # if Esc key is press then break out of the loop
- if key == 27: #The Esc key
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement