-
Notifications
You must be signed in to change notification settings - Fork 32
/
camera_desktop.py
44 lines (39 loc) · 1.22 KB
/
camera_desktop.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
import sys
import cv2
import numpy as np
import pyautogui
if sys.platform == 'linux':
import pyscreenshot as ImageGrab
else:
from PIL import ImageGrab
from PIL import ImageDraw
from base_camera import BaseCamera
class Camera(BaseCamera):
@staticmethod
def frames():
while True:
# capture computer screen
img = ImageGrab.grab()
# draw mouse pointer
img = Camera.draw_mouse(img)
# convert image to numpy array
img_np = np.array(img)
# convert color space from BGR to RGB
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
# convert image to jpg format
ret, jpeg = cv2.imencode('.jpg', frame)
yield jpeg.tobytes()
@staticmethod
def draw_mouse(img):
"""
utility function to draw mouse pointer
"""
# generate Draw object for PIL image
draw = ImageDraw.Draw(img)
# find current position of mouse pointer
pos = pyautogui.position()
# coordinates of ellipse
ax, ay, bx, by = pos[0], pos[1], pos[0]+10, pos[1]+10
# draw ellipse on image
draw.ellipse((ax,ay,bx,by), fill="yellow")
return img