-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan_fridge.py
76 lines (60 loc) · 1.98 KB
/
scan_fridge.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
69
70
71
72
73
74
75
76
'''
Python script for:
1. Taking a webcam capture,
2. Run a object dectection model on the capture
3. Send an email with the detected objects (list & image)
Webcam capture: https://www.geeksforgeeks.org/python-opencv-capture-video-from-camera/
Object Detection: https://stackabuse.com/object-detection-with-imageai-in-python/
Email: https://stackabuse.com/how-to-send-emails-with-gmail-using-python/
'''
import cv2
import datetime
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from imageai.Detection import ObjectDetection
from secrets import gmail_user, gmail_password
# Model setup
detector = ObjectDetection()
model_path = './yolo-tiny.h5'
input_path = './example.jpeg'
output_path = './newimage.jpg'
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(model_path)
detector.loadModel()
# Image capture
vid = cv2.VideoCapture(0)
ret, frame = vid.read()
vid.release()
cv2.imwrite(input_path, frame)
# Model inference
detection = detector.detectObjectsFromImage(input_image=input_path, output_image_path=output_path)
detection_time = datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')
# Mail preparation
detection_items = '\n'.join([f"{item['name']}: {item['percentage_probability']}" for item in detection])
print(detection_items)
img_data = open(output_path, 'rb').read()
msg = MIMEMultipart()
msg['Subject'] = 'Fridge update'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
body = MIMEText(f'''
Hola Claudio! Veo que tu refrigeradora tiene los siguientes productos:
{detection_items}
Última lectura: {detection_time}
''')
msg.attach(body)
image = MIMEImage(img_data, name=os.path.basename(output_path))
msg.attach(image)
# Mail sending
try:
s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
s.login(gmail_user, gmail_password)
s.send_message(msg)
s.close()
print('Email sent!')
except:
print('Something went wrong with the email ...')
raise