Spaces:
Sleeping
Sleeping
Update: Add choices for actions.
Browse files- app.py +27 -11
- configuration.py +1 -1
- inference.py +17 -5
app.py
CHANGED
@@ -7,7 +7,7 @@ from moviepy.video.io.ImageSequenceClip import ImageSequenceClip
|
|
7 |
|
8 |
from configuration import Config
|
9 |
from model import load_classifier, load_detector
|
10 |
-
from inference import format_frame, detect_object, classify_action, draw_boxes
|
11 |
|
12 |
config = Config()
|
13 |
print(f'TensorFlow {tf.__version__}')
|
@@ -20,9 +20,13 @@ classifier.summary()
|
|
20 |
print('Load detector.')
|
21 |
detector = load_detector(config)
|
22 |
|
23 |
-
def fn(video: gr.Video):
|
24 |
print('Process video.')
|
25 |
-
|
|
|
|
|
|
|
|
|
26 |
output = f.name
|
27 |
clip = VideoFileClip(video)
|
28 |
processed_frames = []
|
@@ -31,15 +35,21 @@ def fn(video: gr.Video):
|
|
31 |
detections = ([], [])
|
32 |
for i, frame in enumerate(clip.iter_frames()):
|
33 |
if i % config.classify_action_frame_step == 0:
|
34 |
-
|
|
|
35 |
if i % config.detect_object_frame_step == 0:
|
36 |
print(f'Detect object: Frame {i}')
|
37 |
-
|
|
|
38 |
if len(frames) == config.classify_action_num_frames:
|
39 |
print(f'Classify action: Until frame {i}')
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
processed_frames.append(frame)
|
44 |
if i % config.yield_frame_steps == 0:
|
45 |
quality = 9
|
@@ -53,10 +63,16 @@ def fn(video: gr.Video):
|
|
53 |
processed_clip.write_videofile(output, fps=clip.fps, audio_codec='aac', logger=None)
|
54 |
yield frame, output
|
55 |
|
56 |
-
inputs =
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
outputs = [
|
58 |
-
gr.Image(interactive=False, label='
|
59 |
-
gr.Video(interactive=False, label='
|
60 |
|
61 |
examples = [
|
62 |
['examples/ZFLFDfovqls_001310_001320.mp4'], # cspell: disable-line
|
|
|
7 |
|
8 |
from configuration import Config
|
9 |
from model import load_classifier, load_detector
|
10 |
+
from inference import format_frame, detect_object, classify_action, draw_boxes, draw_classes
|
11 |
|
12 |
config = Config()
|
13 |
print(f'TensorFlow {tf.__version__}')
|
|
|
20 |
print('Load detector.')
|
21 |
detector = load_detector(config)
|
22 |
|
23 |
+
def fn(video: gr.Video, actions: list[int]):
|
24 |
print('Process video.')
|
25 |
+
do_detect = 0 in actions
|
26 |
+
do_classify = 1 in actions
|
27 |
+
if not do_detect and not do_classify:
|
28 |
+
return video
|
29 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as f:
|
30 |
output = f.name
|
31 |
clip = VideoFileClip(video)
|
32 |
processed_frames = []
|
|
|
35 |
detections = ([], [])
|
36 |
for i, frame in enumerate(clip.iter_frames()):
|
37 |
if i % config.classify_action_frame_step == 0:
|
38 |
+
if do_classify:
|
39 |
+
frames.append(format_frame(frame, config))
|
40 |
if i % config.detect_object_frame_step == 0:
|
41 |
print(f'Detect object: Frame {i}')
|
42 |
+
if do_detect:
|
43 |
+
detections = detect_object(detector, frame)
|
44 |
if len(frames) == config.classify_action_num_frames:
|
45 |
print(f'Classify action: Until frame {i}')
|
46 |
+
if do_classify:
|
47 |
+
actions = classify_action(classifier, frames, config.id_to_name)
|
48 |
+
frames = []
|
49 |
+
if do_detect:
|
50 |
+
frame = draw_boxes(frame, detections, actions, do_classify)
|
51 |
+
else:
|
52 |
+
frame = draw_classes(frame, actions)
|
53 |
processed_frames.append(frame)
|
54 |
if i % config.yield_frame_steps == 0:
|
55 |
quality = 9
|
|
|
63 |
processed_clip.write_videofile(output, fps=clip.fps, audio_codec='aac', logger=None)
|
64 |
yield frame, output
|
65 |
|
66 |
+
inputs = [
|
67 |
+
gr.Video(sources=['upload'], label='输入视频片段'),
|
68 |
+
gr.CheckboxGroup(
|
69 |
+
['飞机检测', '飞机行为识别'],
|
70 |
+
label='执行任务',
|
71 |
+
info='可以选择仅执行飞机检测任务或仅执行飞机行为识别任务作为演示。',
|
72 |
+
type='index')]
|
73 |
outputs = [
|
74 |
+
gr.Image(interactive=False, label='最新处理的视频帧'),
|
75 |
+
gr.Video(interactive=False, label='标记飞机位置及行为的视频片段')]
|
76 |
|
77 |
examples = [
|
78 |
['examples/ZFLFDfovqls_001310_001320.mp4'], # cspell: disable-line
|
configuration.py
CHANGED
@@ -29,4 +29,4 @@ class Config:
|
|
29 |
detect_object_frame_step = 5
|
30 |
classify_action_frame_step = frame_step
|
31 |
classify_action_num_frames = num_frames
|
32 |
-
yield_frame_steps =
|
|
|
29 |
detect_object_frame_step = 5
|
30 |
classify_action_frame_step = frame_step
|
31 |
classify_action_num_frames = num_frames
|
32 |
+
yield_frame_steps = 3
|
inference.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import tensorflow as tf
|
2 |
import numpy as np
|
3 |
-
from imgviz import instances2rgb
|
4 |
|
5 |
from configuration import Config
|
6 |
|
@@ -33,7 +33,7 @@ def classify_action(classifier, frames, id_to_name):
|
|
33 |
actions.append(f'{id_to_name[class_id]}: {confidence:.2f}')
|
34 |
return actions
|
35 |
|
36 |
-
def draw_boxes(frame, detections, actions):
|
37 |
(classes, boxes) = detections
|
38 |
max_area = 0
|
39 |
max_area_id = 0
|
@@ -45,9 +45,12 @@ def draw_boxes(frame, detections, actions):
|
|
45 |
labels = [0 for _ in classes]
|
46 |
colormap = [(0x39, 0xc5, 0xbb)]
|
47 |
line_width = 2
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
51 |
bboxes = [
|
52 |
[box[1], box[0], box[3], box[2]]
|
53 |
for box in boxes]
|
@@ -61,6 +64,15 @@ def draw_boxes(frame, detections, actions):
|
|
61 |
line_width=line_width)
|
62 |
return frame
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
def FrameProcessor(detector, classifier, config: Config):
|
65 |
current_frame = 0
|
66 |
frames = []
|
|
|
1 |
import tensorflow as tf
|
2 |
import numpy as np
|
3 |
+
from imgviz import instances2rgb, label2rgb
|
4 |
|
5 |
from configuration import Config
|
6 |
|
|
|
33 |
actions.append(f'{id_to_name[class_id]}: {confidence:.2f}')
|
34 |
return actions
|
35 |
|
36 |
+
def draw_boxes(frame, detections, actions, do_classify):
|
37 |
(classes, boxes) = detections
|
38 |
max_area = 0
|
39 |
max_area_id = 0
|
|
|
45 |
labels = [0 for _ in classes]
|
46 |
colormap = [(0x39, 0xc5, 0xbb)]
|
47 |
line_width = 2
|
48 |
+
if not do_classify:
|
49 |
+
captions = classes
|
50 |
+
else:
|
51 |
+
captions = [
|
52 |
+
f'{class_name}\n' + '\n'.join(actions if i == max_area_id else [])
|
53 |
+
for (i, class_name) in enumerate(classes)]
|
54 |
bboxes = [
|
55 |
[box[1], box[0], box[3], box[2]]
|
56 |
for box in boxes]
|
|
|
64 |
line_width=line_width)
|
65 |
return frame
|
66 |
|
67 |
+
def draw_class(frame, actions):
|
68 |
+
label=['Airplane']
|
69 |
+
label_names = ['Airplane\n' + '\n'.join(actions)]
|
70 |
+
frame = label2rgb(
|
71 |
+
image=frame,
|
72 |
+
label=label,
|
73 |
+
label_names=label_names)
|
74 |
+
return frame
|
75 |
+
|
76 |
def FrameProcessor(detector, classifier, config: Config):
|
77 |
current_frame = 0
|
78 |
frames = []
|