-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
177 lines (156 loc) · 5.68 KB
/
Copy pathserver.py
File metadata and controls
177 lines (156 loc) · 5.68 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import io
import socket
import struct
from PIL import Image
import image_processing.ImageDetection as id
# for opencv
class Server():
def __init__(self):
self.server_socket = socket.socket()
self.server_socket.bind(('192.168.1.38', 8000)) # ADD IP HERE
self.server_socket.listen(0)
# Accept a single connection and make a file-like object out of it
self.connection = self.server_socket.accept()[0].makefile('rb')
# image that will be painted to the screen
self.img = None
self.image_len = None
self.im = None
# when used, put inside try block
def init_image_len(self):
self.image_len = struct.unpack('<L', self.connection.read(struct.calcsize('<L')))[0]
def get_frame(self):
# Construct a stream to hold the image data and read the image
# data from the connection
image_stream = io.BytesIO()
# print("type a:", type(connection.read(image_len)))
image_stream.write(self.connection.read(self.image_len))
# Rewind the stream, open it as an image with PIL and do some
# processing on it
image_stream.seek(0)
print(type(image_stream))
image = Image.open(image_stream)
byteImg = Image.open(image_stream)
print("ASASD", type(byteImg))
path_name = "C:/Users/jryan/PycharmProjects/python video stream/test.jpg"
byteImg.save(path_name, 'JPEG')
# im = Image.frombuffer("I;16", (5, 10), connection.read(image_len), "raw", "I;12")
# print(type(im))
# show yolo-ed image, if it doesn't exist then just show plain image
print('Image type:', type(image))
# matplotlib GUI for debugging
# if self.img is None:
# self.img = image # pl.imshow(image) # image
# else:
# img_path = "C:/Users/jryan/PycharmProjects/python video stream/test_yolo3.jpg"
# self.im = Image.open(img_path)
# self.img.set_data(self.im)
# pl.pause(0.01)
# pl.draw()
print('Image is %dx%d' % image.size)
image.verify()
print('Image type:', type(image))
id.go(image) # run yolo program on the image
print('Image is verified')
# closes the stream (in 'finally' block)
def close_stream(self):
self.connection.close()
self.server_socket.close()
# def run():
# server_socket = socket.socket()
# server_socket.bind(('192.168.1.38', 8000)) # ADD IP HERE
# server_socket.listen(0)
#
# # Accept a single connection and make a file-like object out of it
# connection = server_socket.accept()[0].makefile('rb')
# def get_stream():
# try:
# img = None
# # Read the length of the image as a 32-bit unsigned int. If the
# # length is zero, quit the loop
# image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
# if not image_len:
# break
# # Construct a stream to hold the image data and read the image
# # data from the connection
# image_stream = io.BytesIO()
# # print("type a:", type(connection.read(image_len)))
# image_stream.write(connection.read(image_len))
# # Rewind the stream, open it as an image with PIL and do some
# # processing on it
# image_stream.seek(0)
# print(type(image_stream))
# image = Image.open(image_stream)
#
# byteImg = Image.open(image_stream)
# print("ASASD", type(byteImg))
# byteImg.save('test.jpg', 'JPEG')
#
# # im = Image.frombuffer("I;16", (5, 10), connection.read(image_len), "raw", "I;12")
# # print(type(im))
#
# print('Image type:', type(image))
# if img is None:
# img = pl.imshow(image)
# else:
# im = Image.open("test_yolo3.jpg")
# img.set_data(im)
#
# pl.pause(0.01)
# pl.draw()
#
# print('Image is %dx%d' % image.size)
#
# image.verify()
# print('Image type:', type(image))
# id.go(image) # run yolo program on the image
# print('Image is verified')
# finally:
# connection.close()
# server_socket.close()
#
#
# try:
# img = None
# while True:
# # Read the length of the image as a 32-bit unsigned int. If the
# # length is zero, quit the loop
# image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
# if not image_len:
# break
# # Construct a stream to hold the image data and read the image
# # data from the connection
# image_stream = io.BytesIO()
# # print("type a:", type(connection.read(image_len)))
# image_stream.write(connection.read(image_len))
# # Rewind the stream, open it as an image with PIL and do some
# # processing on it
# image_stream.seek(0)
# print(type(image_stream))
# image = Image.open(image_stream)
#
# byteImg = Image.open(image_stream)
# print("ASASD", type(byteImg))
# byteImg.save('test.jpg', 'JPEG')
#
# # im = Image.frombuffer("I;16", (5, 10), connection.read(image_len), "raw", "I;12")
# # print(type(im))
#
# print('Image type:', type(image))
# if img is None:
# img = pl.imshow(image)
# else:
# im = Image.open("test_yolo3.jpg")
# img.set_data(im)
#
# pl.pause(0.01)
# pl.draw()
#
# print('Image is %dx%d' % image.size)
#
# image.verify()
# print('Image type:', type(image))
# id.go(image) # run yolo program on the image
# print('Image is verified')
# finally:
# connection.close()
# server_socket.close()