-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenZMQmp.py
111 lines (102 loc) · 4.16 KB
/
ScreenZMQmp.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
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
###############################################################################
# Screen Cast
# This program captures desktop screen and sends it to multiple receivers for display
# It is based on examples found here:
# Screen Capture https://www.pyimagesearch.com/2018/01/01/taking-screenshots-with-opencv-and-python/
# ImageZMQ: https://www.pyimagesearch.com/2019/04/15/live-video-streaming-over-network-with-opencv-and-imagezmq/
#
# This code is freeware: no supprt, no warranty, no liability
# Urs Utiznger
# 2020
###############################################################################
# Installation instructions:
#
# In CMD shell:
# pip3 install opencv-contrib-python
# pip3 install imutils
# pip3 install numpy
# pip3 install pillow
# pip3 install mss
# pip3 install pyzmq
# git clone https://github.com/jeffbass/imagezmq.git
# -This shows where the site packages are installed
# python3 -m site --user-site
# Using fileexplorer:
# copy imagezmq/imagezmq.py to where you keep this code
###############################################################################
import imutils
import numpy as np
import cv2
import imagezmq
import argparse
import socket
import time
import PIL
import mss
import mss.tools
from multiprocessing import Process, Queue
jpegQuality = 95
imageDifference = 50000
fpsMax = 15
resolution = 1080
monitor = 0
def grab(queue, monitor):
# type: (Queue) -> None
tickFrequency = cv2.getTickFrequency()
tickDeltaFrame = tickFrequency/fpsMax # ticks between frames
lastTime = cv2.getTickCount()
currentTime = lastTime
with mss.mss() as sct:
while True:
currentTime = cv2.getTickCount()
if (currentTime - lastTime) >= tickDeltaFrame:
lastTime = currentTime
#e1 = cv2.getTickCount()
queue.put(sct.grab(sct.monitors[monitor]))
#e2 = cv2.getTickCount()
#print((e2-e1)/tickFrequency)
if __name__ == "__main__":
# initialize the ImageSender object with the socket address of the server
#sender_0 = imagezmq.ImageSender(connect_to="tcp://{}:5555".format("192.168.8.10"))
#sender_1 = imagezmq.ImageSender(connect_to="tcp://{}:5555".format("192.168.8.11"))
#sender_2 = imagezmq.ImageSender(connect_to="tcp://{}:5555".format("192.168.8.12"))
#sender_3 = imagezmq.ImageSender(connect_to="tcp://{}:5555".format("192.168.8.13"))
# Start screen grab process
queue = Queue() # type: Queue
Process(target=grab, args=(queue, monitor)).start()
# get the host na=me,
hostName = socket.gethostname()
# initialize
numFrames = 0
lastFPSTime = cv2.getTickCount()
tickFrequency = cv2.getTickFrequency()
tickDeltaReport = tickFrequency*5 # ticks for 5 secs
framePrevious = np.array(queue.get())
while True:
# take a screenshot of the screen
frame = np.array(queue.get())
#while not queue.empty():
# queue.get()
# frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # 7ms
# is there enough change on the screen to warrant sending new image?
frameDelta = cv2.absdiff(frame, framePrevious) # 25ms
change = np.sum(frameDelta,dtype=np.int32) # 25ms
if change > imageDifference:
# we want to send new screen capture
framePrevious = frame
# save bandwidth
frameSmall = imutils.resize(frame, height=resolution)
# compress data
retCode, jpgBuffer = cv2.imencode(".jpg", frameSmall, [int(cv2.IMWRITE_JPEG_QUALITY), jpegQuality]) #5ms
#sender_0.send_jpg(hostName, jpgBuffer)
#sender_1.send_jpg(hostName, jpg_buffer)
#sender_2.send_jpg(hostName, jpg_buffer)
#sender_3.send_jpg(hostName, jpg_buffer)
numFrames += 1
currentTime = cv2.getTickCount()
if (currentTime - lastFPSTime) >= tickDeltaReport:
lastFPSTime = currentTime
# report frame rate
print("FPS: {}".format(numFrames/5.0))
numFrames = 0
time.sleep(1.0/(2.0*fpsMax))