Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. # croppfacedetection.py
  2. #Author: Waheed Rafiq
  3. #Research Student Birmingham City University
  4. #Date: 03/11/2016
  5. #Description : Save capture face in a folder.
  6.  
  7. #Import library required for Capture face.
  8. # Should you wish to use this code for
  9. #education purpose in your assignment or dissertation
  10. # please use the correct citation and give credit where required.
  11. from watson_developer_cloud import VisualRecognitionV3
  12. visual_recognition = VisualRecognitionV3(
  13. '2018-03-19',
  14. api_key='d3b2d058a14810b07314d231f2f84c26bb305c8d')
  15. import json
  16. import cv2
  17. import threading
  18. size = 4
  19. webcam = cv2.VideoCapture(0) #Use camera 0
  20.  
  21. # We load the xml file
  22. classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  23. # Above line normalTest
  24. #classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
  25. #Above line test with different calulation
  26. #classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt_tree.xml')
  27. #classifier = cv2.CascadeClassifier('lbpcascade_frontalface.xml')
  28. watsonDone=True
  29. def watsonDoYourJob():
  30. print("watson do you job")
  31. try:
  32. with open('lastHead.jpg', 'rb') as images_file:
  33. faces = visual_recognition.detect_faces(images_file)
  34. print(json.dumps(faces, indent=2))
  35. if __name__ == '__main__':
  36. data = json.dumps(faces, indent=2)
  37.  
  38. data = json.loads(data)
  39.  
  40. minAge = data["images"][0]["faces"][0]
  41. print(minAge)
  42. # cv2.putText(image, "Hello World!!!", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
  43. except Exception as e:
  44. print(e)
  45. global watsonDone
  46. watsonDone=True
  47.  
  48.  
  49. while True:
  50. (rval, im) = webcam.read()
  51. im=cv2.flip(im,1,0) #Flip to act as a mirror
  52.  
  53. # Resize the image to speed up detection
  54. mini = cv2.resize(im, (round(im.shape[1] / size), round(im.shape[0] / size)))
  55.  
  56. # detect MultiScale / faces
  57. faces = classifier.detectMultiScale(mini)
  58. # Draw rectangles around each face
  59. for f in faces:
  60. (x, y, w, h) = [v * size for v in f] #Scale the shapesize backup
  61. cv2.rectangle(im, (x, y), (x + w, y + h),(0,255,0),thickness=4)
  62. #Save just the rectangle faces in SubRecFaces
  63. startX=x-30
  64. if(startX)<0:
  65. startX=0
  66. startY = y - 30
  67. if (startY) < 0:
  68. startY = 0
  69. endX=startX+300
  70. endY = startY + 300
  71. sub_face = im[startY:endY, startX:endX]
  72. FaceFileName = "lastHead.jpg"
  73. cv2.imwrite(FaceFileName, sub_face)
  74. if(watsonDone):
  75. watsonDone=False
  76. threading._start_new_thread(watsonDoYourJob,())
  77.  
  78.  
  79. # Show the image
  80. cv2.imshow('your face', im)
  81. key = cv2.waitKey(10)
  82. # if Esc key is press then break out of the loop
  83. if key == 27: #The Esc key
  84. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement