Advertisement
lollhosh

Untitled

Sep 23rd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1.  
  2. from urllib2 import urlopen
  3.  
  4. from ssl import SSLContext,PROTOCOL_TLSv1
  5.  
  6. import numpy as np
  7.  
  8. # Import OpenCV2 for image processing
  9. import cv2
  10.  
  11. # Detect object in video stream using Haarcascade Frontal Face
  12. face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  13.  
  14. # For each person, one face id
  15. face_id = 1
  16.  
  17. # Initialize sample face image
  18. count = 0
  19.  
  20. # Ip of the IP webcam server (on phone). The phone and your computer must be in the same LAN (connected to the same WiFi)
  21. url = 'https://192.168.15.3:8080/shot.jpg'
  22.  
  23. # Start looping
  24. while(True):
  25.  
  26. # Read the video frame from the url
  27. gcontext = SSLContext(PROTOCOL_TLSv1) # Only for gangstars
  28. info = urlopen(url, context=gcontext).read()
  29.  
  30. imgNp=np.array(bytearray(info),dtype=np.uint8)
  31. image_frame=cv2.imdecode(imgNp,-1)
  32.  
  33. # Convert frame to grayscale
  34. gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY)
  35.  
  36. # Detect frames of different sizes, list of faces rectangles
  37. faces = face_detector.detectMultiScale(gray, 1.3, 5)
  38.  
  39. # Loops for each faces
  40. for (x,y,w,h) in faces:
  41.  
  42. # Crop the image frame into rectangle
  43. cv2.rectangle(image_frame, (x,y), (x+w,y+h), (255,0,0), 2)
  44.  
  45. # Increment sample face image
  46. count += 1
  47.  
  48. # Save the captured image into the datasets folder
  49. cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])
  50.  
  51. print(count)
  52. # Display the video frame, with bounded rectangle on the person's face
  53. cv2.imshow('frame', image_frame)
  54.  
  55. k = cv2.waitKey(33)
  56. if k==27: # Esc key to stop
  57. break
  58.  
  59. # If image taken reach 100, stop taking video
  60. elif count>100:
  61. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement