-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmask.py
59 lines (42 loc) · 1.35 KB
/
mask.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
import cv2
from typing import TypeVar
import numpy
import requests
import sys
from PyInquirer import prompt
T_VideoCapture = TypeVar('cv2.VideoCapture')
T_ndarray = TypeVar('numpy.ndarray')
CAMERA = {
'name' : 'Logitech C920',
'device' : '/dev/video0',
'height' : 720,
'width' : 1080,
'fps' : 30
}
def configure_camera() -> T_VideoCapture:
cap = cv2.VideoCapture(CAMERA['device'])
cap.set(cv2.CAP_PROP_FRAME_WIDTH, CAMERA['width'])
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, CAMERA['height'])
cap.set(cv2.CAP_PROP_FPS, CAMERA['fps'])
return cap
def get_mask(frame : T_ndarray, bodypix_service : str = 'http://localhost:9000') -> T_ndarray:
_, data = cv2.imencode('.jpg', frame)
req = requests.post(
url = bodypix_service,
data = data.tobytes(),
headers = {'Content-Type' : 'application/octet-stream'}
)
mask = numpy.frombuffer(req.content, dtype=numpy.uint8)
mask = mask.reshape((frame.shape[0], frame.shape[1]))
return mask
def capture(cap, filename : str) -> None:
success, frame = cap.read()
mask = get_mask(frame)
width, height = 960, 720
inv_mask = 1 - mask
for c in range(frame.shape[2]):
frame[:, :, c] = frame[:, :, c] * mask
cv2.imwrite(filename, frame)
if __name__ == '__main__':
cap = configure_camera()
capture(cap, 'image.jpg')