sino72 commited on
Commit
114c669
·
1 Parent(s): 134f730

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +19 -9
  2. detectMotion.py +12 -1
  3. sort.py +1 -0
app.py CHANGED
@@ -9,8 +9,7 @@ from sort import * #运动检测,采用sort算法
9
  import tempfile #创建输出临时文件夹
10
  import os
11
  from detectMotion import * #单独的运动检测
12
- import matplotlib
13
- matplotlib.use('Agg')
14
 
15
  #导入YoloV8模型,第一次使用会下载模型到当前文件夹当中
16
  model=YOLO("yolov8n.pt")
@@ -34,6 +33,7 @@ tracker=Sort(max_age=20,min_hits=3,iou_threshold=0.3)
34
 
35
  # 彩色图像进行自适应直方图均衡化
36
  def hisEqulColor(img):
 
37
  ## 将RGB图像转换到YCrCb空间中
38
  ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
39
  # 将YCrCb图像通道分离
@@ -44,7 +44,9 @@ def hisEqulColor(img):
44
  clahe.apply(channels[0], channels[0])
45
  cv2.merge(channels, ycrcb)
46
  cv2.cvtColor(ycrcb, cv2.COLOR_YCR_CB2BGR, img)
47
- return img
 
 
48
 
49
  #添加高斯噪声,并使用中值滤波降噪
50
  def AddGaussNoise(img,sigma):
@@ -57,9 +59,9 @@ def AddGaussNoise(img,sigma):
57
  def processImg(img,sigma):
58
  img=cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
59
  res1 = AddGaussNoise(img,sigma)
60
- res1 = hisEqulColor(res1)
61
  res1=cv2.cvtColor(res1,cv2.COLOR_BGR2RGB)
62
- return res1
63
 
64
  #视频处理
65
  def processVideo(inputPath,codec):
@@ -73,6 +75,7 @@ def processVideo(inputPath,codec):
73
 
74
  #输出格式
75
  if codec == "mp4":
 
76
  fourcc = cv2.VideoWriter_fourcc('m','p','4','v')#视频编码:h264,只有h264格式的mp4文件才能在浏览器直接播放
77
  video_save_path = os.path.join(outputPath,"output.mp4")#创建输出视频路径
78
  elif codec == "avi":
@@ -81,6 +84,9 @@ def processVideo(inputPath,codec):
81
  elif codec == "mkv":
82
  fourcc = cv2.VideoWriter_fourcc('X','V','I','D')#视频编码:XVID,此编码不需要openh264-1.8.0-win64.dll
83
  video_save_path = os.path.join(outputPath,"output.mkv")#创建输出视频路径
 
 
 
84
 
85
  output_viedo.open(video_save_path , fourcc, fps, size, True)
86
  #对每一帧图片进行读取和处理
@@ -90,7 +96,7 @@ def processVideo(inputPath,codec):
90
  detections=np.empty((0, 5))#初始化运动检测
91
  if not(ret):#当视频全部读完,ret返回false,终止循环,视频帧读取和写入结束
92
  break
93
- img = hisEqulColor(img)#视频增强
94
  #读取推理的数据
95
  for r in results:
96
  boxes=r.boxes
@@ -137,13 +143,14 @@ def processVideo(inputPath,codec):
137
  with gr.Blocks() as demo:
138
  gr.Markdown("""
139
  # 运动检测与行人跟踪
140
- 基于opencv + yoloV8 + sort,请输入绝对路径
141
  """)
142
  with gr.Tab("视频识别"):
143
  with gr.Row():
144
  with gr.Column():
145
  text_inputPath = gr.Video()
146
- codec = gr.Radio(["mp4","avi","mkv"], label="输出视频格式")
 
147
  videoProcess_button = gr.Button("处理")
148
  with gr.Column():
149
  text_output = gr.Video()
@@ -154,6 +161,9 @@ with gr.Blocks() as demo:
154
  image_input = gr.Image()
155
  image_sigma = gr.Slider(0,40,label="高斯噪声sigma")
156
  image_output = gr.Image()
 
 
 
157
  image_button = gr.Button("处理")
158
  with gr.Tab("运动检测"):
159
  with gr.Column():
@@ -171,7 +181,7 @@ with gr.Blocks() as demo:
171
 
172
 
173
  videoProcess_button.click(processVideo, inputs=[text_inputPath,codec], outputs=[text_output,text_output_path])
174
- image_button.click(processImg, inputs=[image_input,image_sigma], outputs=image_output)
175
  motionProcess_button.click(motionDetection, inputs=[motion_inputPath], outputs=[motion_output_frame,motion_output_fmask,
176
  frame_output_path,fmask_output_path])
177
 
 
9
  import tempfile #创建输出临时文件夹
10
  import os
11
  from detectMotion import * #单独的运动检测
12
+
 
13
 
14
  #导入YoloV8模型,第一次使用会下载模型到当前文件夹当中
15
  model=YOLO("yolov8n.pt")
 
33
 
34
  # 彩色图像进行自适应直方图均衡化
35
  def hisEqulColor(img):
36
+ fig_preprocessed = image_histogram(img)
37
  ## 将RGB图像转换到YCrCb空间中
38
  ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
39
  # 将YCrCb图像通道分离
 
44
  clahe.apply(channels[0], channels[0])
45
  cv2.merge(channels, ycrcb)
46
  cv2.cvtColor(ycrcb, cv2.COLOR_YCR_CB2BGR, img)
47
+ fig_postprocessed = image_histogram(img)
48
+
49
+ return img, fig_preprocessed, fig_postprocessed
50
 
51
  #添加高斯噪声,并使用中值滤波降噪
52
  def AddGaussNoise(img,sigma):
 
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):
 
75
 
76
  #输出格式
77
  if codec == "mp4":
78
+ #h264:avc1,此处为了兼容性换成mp4v
79
  fourcc = cv2.VideoWriter_fourcc('m','p','4','v')#视频编码:h264,只有h264格式的mp4文件才能在浏览器直接播放
80
  video_save_path = os.path.join(outputPath,"output.mp4")#创建输出视频路径
81
  elif codec == "avi":
 
84
  elif codec == "mkv":
85
  fourcc = cv2.VideoWriter_fourcc('X','V','I','D')#视频编码:XVID,此编码不需要openh264-1.8.0-win64.dll
86
  video_save_path = os.path.join(outputPath,"output.mkv")#创建输出视频路径
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
  #对每一帧图片进行读取和处理
 
96
  detections=np.empty((0, 5))#初始化运动检测
97
  if not(ret):#当视频全部读完,ret返回false,终止循环,视频帧读取和写入结束
98
  break
99
+ img, fig1, fig2= hisEqulColor(img)#视频增强
100
  #读取推理的数据
101
  for r in results:
102
  boxes=r.boxes
 
143
  with gr.Blocks() as demo:
144
  gr.Markdown("""
145
  # 运动检测与行人跟踪
146
+ 基于opencv + yoloV8 + sort
147
  """)
148
  with gr.Tab("视频识别"):
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()
 
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="处理前直方图")
166
+ figure_post_output = gr.Plot(label="处理后直方图")
167
  image_button = gr.Button("处理")
168
  with gr.Tab("运动检测"):
169
  with gr.Column():
 
181
 
182
 
183
  videoProcess_button.click(processVideo, inputs=[text_inputPath,codec], outputs=[text_output,text_output_path])
184
+ image_button.click(processImg, inputs=[image_input,image_sigma], outputs=[image_output,figure_pre_output,figure_post_output])
185
  motionProcess_button.click(motionDetection, inputs=[motion_inputPath], outputs=[motion_output_frame,motion_output_fmask,
186
  frame_output_path,fmask_output_path])
187
 
detectMotion.py CHANGED
@@ -3,6 +3,9 @@ import cv2
3
  import numpy as np
4
  import tempfile
5
  import os
 
 
 
6
 
7
  kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
8
  mog = cv2.createBackgroundSubtractorMOG2() # 创建混合高斯模型来用于北京建模
@@ -15,7 +18,7 @@ def motionDetection(inputPath):
15
  output_viedo_frame = cv2.VideoWriter()#初始化视频写入
16
  output_viedo_fmask = cv2.VideoWriter()#初始化视频写入
17
  outputPath=tempfile.mkdtemp()#创建输出视频的临时文件夹的路径
18
- fourcc = cv2.VideoWriter_fourcc('m','p','4','v')#视频编码:h264,只有h264格式的mp4文件才能在浏览器直接播放
19
  video_save_path_frame = os.path.join(outputPath,"frame.mp4")#创建输出视频路径
20
  video_save_path_fmask = os.path.join(outputPath,"fmask.mp4")#创建输出视频路径
21
  output_viedo_frame.open(video_save_path_frame , fourcc, fps, size, True)
@@ -53,7 +56,15 @@ def motionDetection(inputPath):
53
  return video_save_path_frame,video_save_path_fmask,video_save_path_frame,video_save_path_fmask
54
 
55
 
 
 
 
 
 
 
56
 
 
 
57
 
58
 
59
 
 
3
  import numpy as np
4
  import tempfile
5
  import os
6
+ import matplotlib
7
+ import matplotlib.pyplot as plt
8
+ matplotlib.use('Agg')
9
 
10
  kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
11
  mog = cv2.createBackgroundSubtractorMOG2() # 创建混合高斯模型来用于北京建模
 
18
  output_viedo_frame = cv2.VideoWriter()#初始化视频写入
19
  output_viedo_fmask = cv2.VideoWriter()#初始化视频写入
20
  outputPath=tempfile.mkdtemp()#创建输出视频的临时文件夹的路径
21
+ fourcc = cv2.VideoWriter_fourcc('a','v','c','1')#视频编码:h264,只有h264格式的mp4文件才能在浏览器直接播放
22
  video_save_path_frame = os.path.join(outputPath,"frame.mp4")#创建输出视频路径
23
  video_save_path_fmask = os.path.join(outputPath,"fmask.mp4")#创建输出视频路径
24
  output_viedo_frame.open(video_save_path_frame , fourcc, fps, size, True)
 
56
  return video_save_path_frame,video_save_path_fmask,video_save_path_frame,video_save_path_fmask
57
 
58
 
59
+ #下面是直方图相关
60
+ def image_histogram(img):
61
+ plt.close()
62
+ fig = plt.figure(figsize=(15,12))
63
+ plt.hist(img.ravel(),256, [0, 255]);
64
+ return fig
65
 
66
+ #img = cv2.imread("C:\\Users\\sino\\Pictures\\fan.jpg")
67
+ #image_histogram(img)
68
 
69
 
70
 
sort.py CHANGED
@@ -20,6 +20,7 @@ from __future__ import print_function
20
  import os
21
  import numpy as np
22
  import matplotlib
 
23
  import matplotlib.pyplot as plt
24
  import matplotlib.patches as patches
25
  from skimage import io
 
20
  import os
21
  import numpy as np
22
  import matplotlib
23
+ #matplotlib.use('TkAgg')
24
  import matplotlib.pyplot as plt
25
  import matplotlib.patches as patches
26
  from skimage import io