-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
69 lines (54 loc) · 1.46 KB
/
camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from picamera import PiCamera
from time import sleep
import os, os.path
import socket
import numpy as np
import cv2 as cv
# initialize classifiers
face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml')
# Count the number of files in folder at the start of script
count = len([name for name in os.listdir(".")])
# Connect to the camera
camera = PiCamera()
camera.start_preview()
# Set server ip and port for the server you want to connect to
host = '192.168.87.105'
port = 60000
i = count
while True:
# Make socket
s = socket.socket()
s.connect((host,port))
file_name = "./image" + str(i) + ".jpg"
sleep(5)
# Take picture
camera.capture(file_name)
print("Captured: " + file_name)
# Analyse with openCV
img = cv.imread(file_name)
print(img)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv.imwrite(file_name,img)
# Send file
f = open(file_name, 'rb')
l = f.read(1024)
while(l):
s.send(l)
l = f.read(1024)
# Close file
f.close()
i+=1
print("File sent")
# Close socket
s.close()
camera.stop_preview()
print( "Done sending image")