-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
102 lines (82 loc) · 2.34 KB
/
main.py
File metadata and controls
102 lines (82 loc) · 2.34 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""
@author: Neel
"""
import cv2
import time
import base64
import requests
import serial
import mysql.connector
def detect_faces(path):
from google.cloud import vision
import io
client = vision.ImageAnnotatorClient()
exist = 0
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.face_detection(image=image)
faces = response.face_annotations
if faces:
exist = 1
else:
exist = 0
return exist
def insert(link):
db = mysql.connector.connect(
host = "remotemysql.com",
user = "hwW4R6cA0s",
password = "9bVe4xsxvX",
database = "hwW4R6cA0s"
)
cursor = db.cursor()
sql = "INSERT INTO SecuRest (link) VALUES ('"+link+"')"
val = str(link)
cursor.execute(sql, val)
db.commit()
distance = serial.Serial('COM5')
distance.flushInput()
iter = 0
err_count = 0
serial_iter = 0
flag = 0
cam = cv2.VideoCapture(0)
while True:
try:
ser = distance.readline()
decoded = float(ser[0:len(ser)-2].decode("utf-8"))
print(decoded)
serial_iter += 1
if decoded < 10 and serial_iter > 2:
distance.write("M".encode())
flag = 1
ret, frame = cam.read()
cv2.imshow('frame', frame)
iter += 1
if iter <= 5:
time.sleep(1)
cv2.imwrite(str(iter)+".jpg", frame)
exist = detect_faces(str(iter)+".jpg")
if exist == 1:
with open(str(iter)+".jpg", "rb") as file:
url = "https://api.imgbb.com/1/upload"
payload = {
"key": "4e8e6f9baef8b46f75ac078d4bded8c1",
"image": base64.b64encode(file.read()),
}
res = requests.post(url, payload)
dict = res.json()
url = dict['data']['url']
insert(url)
else:
print("No face")
else:
break
except:
err_count += 1
if err_count > 100:
break
cam.release()
cv2.destroyAllWindows()
distance.close()