Spaces:
Running
Running
Upload 5 files
Browse files- EDSR_x4.pb +3 -0
- app.py +61 -9
- openh264-1.8.0-win64.dll +0 -0
- requirements.txt +5 -3
EDSR_x4.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:dd35ce3cae53ecee2d16045e08a932c3e7242d641bb65cb971d123e06904347f
|
3 |
+
size 38573255
|
app.py
CHANGED
@@ -9,10 +9,14 @@ from sort import * #运动检测,采用sort算法
|
|
9 |
import tempfile #创建输出临时文件夹
|
10 |
import os
|
11 |
from detectMotion import * #单独的运动检测
|
|
|
12 |
|
13 |
|
14 |
#导入YoloV8模型,第一次使用会下载模型到当前文件夹当中
|
15 |
model=YOLO("yolov8n.pt")
|
|
|
|
|
|
|
16 |
|
17 |
#YoloV8标签数据,本次项目只使用了'person'
|
18 |
classNames=['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
|
@@ -31,6 +35,18 @@ classNames=['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train'
|
|
31 |
#运动检测算法参数
|
32 |
tracker=Sort(max_age=20,min_hits=3,iou_threshold=0.3)
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# 彩色图像进行自适应直方图均衡化
|
35 |
def hisEqulColor(img):
|
36 |
fig_preprocessed = image_histogram(img)
|
@@ -48,23 +64,40 @@ def hisEqulColor(img):
|
|
48 |
|
49 |
return img, fig_preprocessed, fig_postprocessed
|
50 |
|
51 |
-
|
52 |
def AddGaussNoise(img,sigma):
|
53 |
gauss=np.random.normal(0,sigma,img.shape)
|
54 |
img=np.uint8(img + gauss)#将高斯噪声与原始图像叠加
|
55 |
-
img=cv2.medianBlur(img,5)
|
56 |
return img
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
#图像处理
|
59 |
-
def processImg(img,sigma):
|
60 |
img=cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
|
61 |
res1 = AddGaussNoise(img,sigma)
|
|
|
|
|
|
|
62 |
res1, fig1, fig2 = hisEqulColor(res1)
|
|
|
|
|
63 |
res1=cv2.cvtColor(res1,cv2.COLOR_BGR2RGB)
|
64 |
return res1, fig1, fig2
|
65 |
|
66 |
#视频处理
|
67 |
def processVideo(inputPath,codec):
|
|
|
|
|
|
|
|
|
68 |
number_of_people=0
|
69 |
cap = cv2.VideoCapture(inputPath)#从inputPath读入视频
|
70 |
fps = cap.get(cv2.CAP_PROP_FPS) #获取视频的帧率
|
@@ -87,6 +120,10 @@ def processVideo(inputPath,codec):
|
|
87 |
elif codec == "wmv":
|
88 |
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')#视频编码:XVID,此编码不需要openh264-1.8.0-win64.dll
|
89 |
video_save_path = os.path.join(outputPath,"output.wmv")#创建输出视频路径
|
|
|
|
|
|
|
|
|
90 |
|
91 |
output_viedo.open(video_save_path , fourcc, fps, size, True)
|
92 |
#对每一帧图片进行读取和处理
|
@@ -108,11 +145,15 @@ def processVideo(inputPath,codec):
|
|
108 |
cls=int(box.cls[0])#获取物体类别标签
|
109 |
#当标签为人,且可信度大于0.3的时候,将人标识出来
|
110 |
if cls==0 and conf > 0.3:
|
|
|
111 |
cv2.rectangle(img,(x1,y1),(x2,y2),(255,0,255),3)
|
112 |
-
print(conf)
|
113 |
#cvzone.putTextRect(img,f'{classNames[cls]}{conf}',(max(0,x1),max(30,y1)),scale=0.7,thickness=1)
|
114 |
currentArray=np.array([x1,y1,x2,y2,conf])
|
115 |
detections=np.vstack((detections,currentArray))#按行堆叠数据
|
|
|
|
|
|
|
116 |
#运动检测
|
117 |
resultsTracker=tracker.update(detections)
|
118 |
for result in resultsTracker:
|
@@ -125,6 +166,7 @@ def processVideo(inputPath,codec):
|
|
125 |
output_viedo.write(img)#将处理后的图像写入视频
|
126 |
output_viedo.release()#释放
|
127 |
cap.release()#释放
|
|
|
128 |
return video_save_path,video_save_path
|
129 |
|
130 |
|
@@ -149,17 +191,23 @@ with gr.Blocks() as demo:
|
|
149 |
with gr.Row():
|
150 |
with gr.Column():
|
151 |
text_inputPath = gr.Video()
|
152 |
-
codec = gr.Radio(["mp4","avi","mkv","wmv"], label="输出视频格式",
|
153 |
value="mp4")
|
154 |
videoProcess_button = gr.Button("处理")
|
155 |
with gr.Column():
|
156 |
text_output = gr.Video()
|
157 |
text_output_path = gr.Text(label="输出路径")
|
|
|
|
|
|
|
|
|
158 |
with gr.Tab("图像增强"):
|
159 |
with gr.Row():
|
160 |
with gr.Column():
|
161 |
image_input = gr.Image()
|
162 |
image_sigma = gr.Slider(0,40,label="高斯噪声sigma")
|
|
|
|
|
163 |
image_output = gr.Image()
|
164 |
with gr.Column():
|
165 |
figure_pre_output = gr.Plot(label="处理前直方图")
|
@@ -180,10 +228,14 @@ with gr.Blocks() as demo:
|
|
180 |
|
181 |
|
182 |
|
183 |
-
videoProcess_button.click(processVideo, inputs=[text_inputPath,codec],
|
184 |
-
|
185 |
-
|
186 |
-
|
|
|
|
|
|
|
|
|
187 |
|
188 |
demo.queue()#当有多个请求时,排队
|
189 |
demo.launch()#生成内网链接,如需要公网链接,括号内输入share=True
|
|
|
9 |
import tempfile #创建输出临时文件夹
|
10 |
import os
|
11 |
from detectMotion import * #单独的运动检测
|
12 |
+
import time
|
13 |
|
14 |
|
15 |
#导入YoloV8模型,第一次使用会下载模型到当前文件夹当中
|
16 |
model=YOLO("yolov8n.pt")
|
17 |
+
global number_of_people_in_one_frame_list, flag
|
18 |
+
number_of_people_in_one_frame_list = None
|
19 |
+
flag = False
|
20 |
|
21 |
#YoloV8标签数据,本次项目只使用了'person'
|
22 |
classNames=['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
|
|
|
35 |
#运动检测算法参数
|
36 |
tracker=Sort(max_age=20,min_hits=3,iou_threshold=0.3)
|
37 |
|
38 |
+
def plot_number():
|
39 |
+
global number_of_people_in_one_frame_list, flag
|
40 |
+
if flag == False:
|
41 |
+
time.sleep(5)
|
42 |
+
flag = True
|
43 |
+
if number_of_people_in_one_frame_list == None:
|
44 |
+
raise gr.Error("请先选择需要处理的视频")
|
45 |
+
plt.close()
|
46 |
+
fig = plt.figure()
|
47 |
+
plt.plot(number_of_people_in_one_frame_list)
|
48 |
+
return fig
|
49 |
+
|
50 |
# 彩色图像进行自适应直方图均衡化
|
51 |
def hisEqulColor(img):
|
52 |
fig_preprocessed = image_histogram(img)
|
|
|
64 |
|
65 |
return img, fig_preprocessed, fig_postprocessed
|
66 |
|
67 |
+
#添加高斯噪声
|
68 |
def AddGaussNoise(img,sigma):
|
69 |
gauss=np.random.normal(0,sigma,img.shape)
|
70 |
img=np.uint8(img + gauss)#将高斯噪声与原始图像叠加
|
71 |
+
#img=cv2.medianBlur(img,5)
|
72 |
return img
|
73 |
|
74 |
+
#图像超分
|
75 |
+
def AddISR(img):
|
76 |
+
sr = cv2.dnn_superres.DnnSuperResImpl_create()
|
77 |
+
sr.readModel("EDSR_x4.pb")
|
78 |
+
sr.setModel("edsr", 4) # set the model by passing the value and the upsampling ratio
|
79 |
+
result = sr.upsample(img) # upscale the input image
|
80 |
+
return result
|
81 |
+
|
82 |
#图像处理
|
83 |
+
def processImg(img,sigma,median_filter,ISR):
|
84 |
img=cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
|
85 |
res1 = AddGaussNoise(img,sigma)
|
86 |
+
if median_filter == True:
|
87 |
+
#中值滤波降噪
|
88 |
+
img=cv2.medianBlur(img,5)
|
89 |
res1, fig1, fig2 = hisEqulColor(res1)
|
90 |
+
if ISR == True:
|
91 |
+
res1 = AddISR(res1)
|
92 |
res1=cv2.cvtColor(res1,cv2.COLOR_BGR2RGB)
|
93 |
return res1, fig1, fig2
|
94 |
|
95 |
#视频处理
|
96 |
def processVideo(inputPath,codec):
|
97 |
+
global number_of_people_in_one_frame_list, flag
|
98 |
+
flag = False
|
99 |
+
number_of_people_in_one_frame = 0
|
100 |
+
number_of_people_in_one_frame_list = []
|
101 |
number_of_people=0
|
102 |
cap = cv2.VideoCapture(inputPath)#从inputPath读入视频
|
103 |
fps = cap.get(cv2.CAP_PROP_FPS) #获取视频的帧率
|
|
|
120 |
elif codec == "wmv":
|
121 |
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')#视频编码:XVID,此编码不需要openh264-1.8.0-win64.dll
|
122 |
video_save_path = os.path.join(outputPath,"output.wmv")#创建输出视频路径
|
123 |
+
elif codec == "mp4(h264)":
|
124 |
+
#h264:avc1, 在本地运行时可以在浏览器播放
|
125 |
+
fourcc = cv2.VideoWriter_fourcc('a','v','c','1')#视频编码:h264,只有h264格式的mp4文件才能在浏览器直接播放
|
126 |
+
video_save_path = os.path.join(outputPath,"output.mp4")#创建输出视频路径
|
127 |
|
128 |
output_viedo.open(video_save_path , fourcc, fps, size, True)
|
129 |
#对每一帧图片进行读取和处理
|
|
|
145 |
cls=int(box.cls[0])#获取物体类别标签
|
146 |
#当标签为人,且可信度大于0.3的时候,将人标识出来
|
147 |
if cls==0 and conf > 0.3:
|
148 |
+
number_of_people_in_one_frame += 1
|
149 |
cv2.rectangle(img,(x1,y1),(x2,y2),(255,0,255),3)
|
150 |
+
#print(conf)
|
151 |
#cvzone.putTextRect(img,f'{classNames[cls]}{conf}',(max(0,x1),max(30,y1)),scale=0.7,thickness=1)
|
152 |
currentArray=np.array([x1,y1,x2,y2,conf])
|
153 |
detections=np.vstack((detections,currentArray))#按行堆叠数据
|
154 |
+
number_of_people_in_one_frame_list.append(number_of_people_in_one_frame)
|
155 |
+
#print(type(number_of_people_in_one_frame_list))
|
156 |
+
number_of_people_in_one_frame = 0
|
157 |
#运动检测
|
158 |
resultsTracker=tracker.update(detections)
|
159 |
for result in resultsTracker:
|
|
|
166 |
output_viedo.write(img)#将处理后的图像写入视频
|
167 |
output_viedo.release()#释放
|
168 |
cap.release()#释放
|
169 |
+
flag = False
|
170 |
return video_save_path,video_save_path
|
171 |
|
172 |
|
|
|
191 |
with gr.Row():
|
192 |
with gr.Column():
|
193 |
text_inputPath = gr.Video()
|
194 |
+
codec = gr.Radio(["mp4","avi","mkv","wmv",'mp4(h264)'], label="输出视频格式",
|
195 |
value="mp4")
|
196 |
videoProcess_button = gr.Button("处理")
|
197 |
with gr.Column():
|
198 |
text_output = gr.Video()
|
199 |
text_output_path = gr.Text(label="输出路径")
|
200 |
+
with gr.Row():
|
201 |
+
with gr.Column():
|
202 |
+
figure_number_output = gr.Plot(label="人流量")
|
203 |
+
#plotProcess_button = gr.Button("人流量显示")
|
204 |
with gr.Tab("图像增强"):
|
205 |
with gr.Row():
|
206 |
with gr.Column():
|
207 |
image_input = gr.Image()
|
208 |
image_sigma = gr.Slider(0,40,label="高斯噪声sigma")
|
209 |
+
median_filter = gr.Checkbox(label="中值滤波")
|
210 |
+
Add_ISR = gr.Checkbox(label="图像超分")
|
211 |
image_output = gr.Image()
|
212 |
with gr.Column():
|
213 |
figure_pre_output = gr.Plot(label="处理前直方图")
|
|
|
228 |
|
229 |
|
230 |
|
231 |
+
videoProcess_button.click(processVideo, inputs=[text_inputPath,codec],
|
232 |
+
outputs=[text_output,text_output_path])
|
233 |
+
image_button.click(processImg, inputs=[image_input,image_sigma,median_filter,Add_ISR],
|
234 |
+
outputs=[image_output,figure_pre_output,figure_post_output])
|
235 |
+
motionProcess_button.click(motionDetection, inputs=[motion_inputPath],
|
236 |
+
outputs=[motion_output_frame,motion_output_fmask,
|
237 |
+
frame_output_path,fmask_output_path])
|
238 |
+
videoProcess_button.click(plot_number,outputs=figure_number_output,every=1)
|
239 |
|
240 |
demo.queue()#当有多个请求时,排队
|
241 |
demo.launch()#生成内网链接,如需要公网链接,括号内输入share=True
|
openh264-1.8.0-win64.dll
ADDED
Binary file (825 kB). View file
|
|
requirements.txt
CHANGED
@@ -2,7 +2,9 @@ ultralytics
|
|
2 |
opencv-python
|
3 |
cvzone
|
4 |
torch
|
|
|
|
|
5 |
matplotlib
|
6 |
-
filterpy
|
7 |
-
scikit-image
|
8 |
-
lap
|
|
|
2 |
opencv-python
|
3 |
cvzone
|
4 |
torch
|
5 |
+
gradio
|
6 |
+
opencv-contrib-python
|
7 |
matplotlib
|
8 |
+
filterpy
|
9 |
+
scikit-image
|
10 |
+
lap
|