-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSender.py
73 lines (57 loc) · 1.79 KB
/
Sender.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
import zmq
import pyaudio
from cv2 import VideoCapture
import cv2
import numpy
import struct
import bz2
import time
import sys
ip = "0.0.0.0"
port = 8000
audio = 0
try:
ip = sys.argv[1]
port = sys.argv[2]
except:
print("Please include the sender IP, Port")
exit()
camNumber = 0
PyAudio = pyaudio.PyAudio()
context = zmq.Context()
PublishSocket = context.socket(zmq.PUB)
PublishSocket.set_hwm(2) #Set ZMQ high water mark
PublishSocket.bind("tcp://"+ip+":%s" % port) # Bind server to ip
capture = VideoCapture(camNumber) #Video Source
def Audio(in_data,frame_count,time_info,status_flag):
PublishSocket.send(bz2.compress(b"A"+in_data))
return (None,0)
stream = PyAudio.open(8000,1,pyaudio.paFloat32 ,True,False,None,None,256,True,None,None,Audio)
def AddString(frame1,string):
cv2.putText(frame1,string,(0,25),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),2, cv2.LINE_AA)
return frame1
frames = 0
fps = 30
timea = time.time()
bytes = 0
bytesPerSec = 0
while True:
buffer = b"V"
frames +=1
ret,raw_frame = capture.read() #Read camera
frame = cv2.resize(raw_frame,(320,240)) #Downsize the video frame
frameBytes = frame.tobytes() #Convert it to bytes
buffer += struct.pack("HH",frame.shape[0],frame.shape[1]) #Pack frame size into packet
buffer += frameBytes #Add frame data to packet
compressed = bz2.compress(buffer,9)
PublishSocket.send(compressed) #Send Packet
bytes += len(compressed)
if(time.time() - timea > 1):
timea = time.time()
fps = frames
frames = 0
bytesPerSec += bytes/1024/1024
bytesPerSec = bytesPerSec/2
bytes = 0
cv2.imshow("Video",AddString(raw_frame,str(fps)+" FPS " + "{:04.2f} Avg MBytes Per Second".format(bytesPerSec))) #Display the frame
cv2.waitKey(1)