-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer_canny.py
86 lines (61 loc) · 1.86 KB
/
streamer_canny.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
77
78
79
80
81
82
83
84
85
86
import cv2
import threading
import socket
import struct
import io
import json
import numpy
import pickle
class Streamer_canny (threading.Thread):
def __init__(self, hostname, port):
threading.Thread.__init__(self)
self.hostname = hostname
self.port = port
self.connected = False
self.jpeg = None
def run(self):
self.isRunning = True
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')
s.bind((self.hostname, self.port))
print('Socket bind complete')
data = b""
payload_size = struct.calcsize("L")
s.listen(10)
print('Socket now listening')
while self.isRunning:
conn, addr = s.accept()
while True:
while len(data) < payload_size:
data += conn.recv(4096)
# data = conn.recv(4096)
if data:
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("L", packed_msg_size)[0]
while len(data) < msg_size:
data += conn.recv(4096)
frame_data = data[:msg_size]
# memfile = io.BytesIO()
# memfile.write(json.loads(frame_data).encode('latin-1'))
# # memfile.write(json.loads(frame_data))
# memfile.seek(0)
# frame = numpy.load(memfile)
data = data[msg_size:]
frame = pickle.loads(frame_data, encoding='latin1')
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.Canny(frame, 100, 200)
ret, jpeg = cv2.imencode('.jpg', frame)
self.jpeg = jpeg
self.connected = True
else:
conn.close()
self.connected = False
break
self.connected = False
def stop(self):
self.isRunning = False
def client_connected(self):
return self.connected
def get_jpeg(self):
return self.jpeg.tobytes()