-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
153 lines (106 loc) · 3.74 KB
/
Main.py
File metadata and controls
153 lines (106 loc) · 3.74 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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
#!pip install opencv-python==3.4.16.57
#參考資料 https://stackoverflow.com/questions/41385708/multiprocessing-example-giving-attributeerror
from multiprocessing import shared_memory, Value, Process
import os, signal, platform
import time
import Camera # 1. Open Camera, copy frame to shared memory
import HttpServer # 3. Create Http Server for index.html, port 8080
import MediaRecorder # 2. Record Video to Http Server via ffmpeg
CameraIsOpen = Value("i", 1)
img, imgInfo, height, width, flag, r, g, b, videos = [0]*9
max_file = 10
# In[ ]:
def EvenListener():
while 1:
time.sleep(0.1)
if flag.buf[0:1]==b"0" or CameraIsOpen.value==0:
raise I_WILL_BROKEN_IT
def getInfo():
global img, imgInfo, width, height, flag, videos, r, g, b
import cv2
import numpy as np
cv2.destroyAllWindows()
img = 0
shape_img = 0
size = 0
W_and_H = 0
typ = 0
# GET IMG WIDTH AND HEIGHT
video = cv2.VideoCapture(0)
height = int(video.get(4))
width = int(video.get(3))
fps = int(video.get(5))
video.release()
shape_img = (height,width,3)
size = np.zeros(shape_img,dtype=np.uint8).nbytes
typ = np.uint8
W_and_H = (width,height)
cv2.destroyAllWindows()
info = np.array([shape_img[0],shape_img[1],shape_img[2]],dtype=np.int32)
imgInfo = shared_memory.SharedMemory(create=True, size=info.nbytes, name="imgInfo")
tmp = np.ndarray((3,), info.dtype, buffer=imgInfo.buf)
tmp[:] = info[:]
del tmp
cv2.destroyAllWindows()
img = shared_memory.SharedMemory(create=True, size=size, name="img")
flag = shared_memory.SharedMemory(create=True, size=10, name="flag")
flag.buf[0:1]=b"1"
r = shared_memory.SharedMemory(create=True, size=5, name='r')
g = shared_memory.SharedMemory(create=True, size=5, name='g')
b = shared_memory.SharedMemory(create=True, size=5, name='b')
r.buf[0:5]=b"+0000"
g.buf[0:5]=b"+0000"
b.buf[0:5]=b"+0000"
videos = [shared_memory.SharedMemory(create=True, size=500*1024, name=("video"+str(_)) ) for _ in range(max_file)] # 500 kb
# In[ ]:
def main():
print("注意: \n 運行此Python時,請確保在CMD運行Python的當前工作資料夾和此\"Main.py\"檔案的位置相同,否則將會運行失敗。")
print("注意: \n Server正在準備開啟。若要停止 Server,請至 Server 啟動後的網頁按下 Shutdown Server 按鈕,或在 cmd 按下 Ctrl+C 。")
getInfo()
camera = Process(target=Camera.main, args=(CameraIsOpen,) )
camera.start()
recorder = Process(target=MediaRecorder.main, args=() )
recorder.start()
host = "127.0.0.1"
port = 8080
httpserver = Process(target=HttpServer.main, args=(host, port, height, width) )
httpserver.start()
time.sleep(1)
print("\nServer Is Starting, Wait 1 Minutes please.\n")
try:
import webbrowser
webbrowser.open("http://{}:{}".format(host,port), new=0, autoraise=True)
EvenListener()
except:
CameraIsOpen.value = 0
imgInfo.close()
imgInfo.unlink()
img.close()
img.unlink()
flag.close()
flag.unlink()
r.close()
r.unlink()
g.close()
g.unlink()
b.close()
b.unlink()
for _ in videos :
_.close()
_.unlink()
camera.terminate()
recorder.terminate()
httpserver.terminate()
camera.join()
recorder.join()
httpserver.join()
camera.close()
recorder.close()
httpserver.close()
print("\nFinish\n")
# In[ ]:
if __name__=="__main__":
main()