-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
76 lines (57 loc) · 1.92 KB
/
client.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
import cv2
import base64
import socket
import numpy as np
from pprint import pprint
from io import StringIO
from kafka import KafkaProducer
import pickle
import time
import os
import sys
import json
# import dlib
from datetime import datetime
import imutils
from dotenv import load_dotenv
load_dotenv()
topic = os.getenv("TOPIC", 'test')
def show_webcam(mirror=False, rotate=0):
producer = KafkaProducer(bootstrap_servers=os.getenv('SERVER-IP', 'localhost') + ':' + os.getenv('PORT', '9092'))
print("Conectado al servidor kafka.")
cam = cv2.VideoCapture(0)
print("Conectado a la camara.")
while True:
#cam.set(3, 1920)
#cam.set(4, 1080)
ret_val, img = cam.read()
if rotate != 0:
img = imutils.rotate_bound(img, 180)
if mirror:
img = cv2.flip(img, 1)
# img = cv2.resize(img, (1920, 1080))
h, w, rrr = img.shape
# print('I am sending: {}, w: {}, h: {}'.format(datetime.now(), w, h))
tm = datetime.now()
# Round date to 0 or 5
# try:
# tm = tm.replace(second=tm.second - (tm.second % 5) if (tm.second % 5) < 3 else tm.second + (5 - (tm.second % 5)))
# except Exception as e:
# tm.replace(minute=tm.minute+1 if tm.minute%59 != 0 else tm.minute)
# tm.replace(second=0)
timestamp = tm.strftime("%Y-%m-%d %H:%M:%S").encode('utf8')
to_send = cv2.imencode('.jpg', img)[1].tostring()
producer.send(topic, to_send, key=timestamp)
k = cv2.waitKey(1)
if k == 27:
break # esc to quit
cam.release()
cv2.destroyAllWindows()
# This is a redundant function, not necesarry if you implement if __name__ == '__main__'
#def main():
# show_webcam(mirror=False)
if __name__ == '__main__':
try:
show_webcam(mirror=False, rotate=0)
except KeyboardInterrupt:
print("Ha habdo un error en la ejecución.")