Spaces:
Sleeping
Sleeping
test
Browse files- app.py +7 -5
- configuration.py +4 -4
- inference.py +5 -9
- model.py +0 -4
app.py
CHANGED
@@ -8,11 +8,13 @@ from moviepy.video.io.ImageSequenceClip import ImageSequenceClip
|
|
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 |
config = Config()
|
12 |
print(f'TensorFlow {tf.__version__}')
|
13 |
|
14 |
print(f'Load classifier from {config.classifier_path}')
|
15 |
classifier = load_classifier(config)
|
|
|
16 |
classifier.summary()
|
17 |
|
18 |
print('Load detector.')
|
@@ -28,9 +30,9 @@ def fn(video: gr.Video):
|
|
28 |
actions = []
|
29 |
detections = ([], [])
|
30 |
for i, frame in enumerate(clip.iter_frames()):
|
31 |
-
if i % config.
|
32 |
frames.append(format_frame(frame, config))
|
33 |
-
if i % config.
|
34 |
print(f'Detect object: Frame {i}')
|
35 |
detections = detect_object(detector, frame)
|
36 |
if len(frames) == config.classify_action_num_frames:
|
@@ -40,10 +42,10 @@ def fn(video: gr.Video):
|
|
40 |
frame = draw_boxes(frame, detections, actions)
|
41 |
processed_frames.append(frame)
|
42 |
if i % config.yield_frame_steps == 0:
|
|
|
|
|
|
|
43 |
with tempfile.NamedTemporaryFile(suffix='.jpeg') as f:
|
44 |
-
quality = 9
|
45 |
-
image_array = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
46 |
-
_, image_encoded = cv2.imencode('.jpg', image_array, [int(cv2.IMWRITE_JPEG_QUALITY), quality])
|
47 |
f.write(image_encoded)
|
48 |
yield f.name, None
|
49 |
processed_clip = ImageSequenceClip(processed_frames, clip.fps)
|
|
|
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__}')
|
14 |
|
15 |
print(f'Load classifier from {config.classifier_path}')
|
16 |
classifier = load_classifier(config)
|
17 |
+
classifier.trainable = False
|
18 |
classifier.summary()
|
19 |
|
20 |
print('Load detector.')
|
|
|
30 |
actions = []
|
31 |
detections = ([], [])
|
32 |
for i, frame in enumerate(clip.iter_frames()):
|
33 |
+
if i % config.classify_action_frame_step == 0:
|
34 |
frames.append(format_frame(frame, config))
|
35 |
+
if i % config.detect_object_frame_step == 0:
|
36 |
print(f'Detect object: Frame {i}')
|
37 |
detections = detect_object(detector, frame)
|
38 |
if len(frames) == config.classify_action_num_frames:
|
|
|
42 |
frame = draw_boxes(frame, detections, actions)
|
43 |
processed_frames.append(frame)
|
44 |
if i % config.yield_frame_steps == 0:
|
45 |
+
quality = 9
|
46 |
+
image_array = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
47 |
+
_, image_encoded = cv2.imencode('.jpg', image_array, [int(cv2.IMWRITE_JPEG_QUALITY), quality])
|
48 |
with tempfile.NamedTemporaryFile(suffix='.jpeg') as f:
|
|
|
|
|
|
|
49 |
f.write(image_encoded)
|
50 |
yield f.name, None
|
51 |
processed_clip = ImageSequenceClip(processed_frames, clip.fps)
|
configuration.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
class Config:
|
2 |
-
num_frames =
|
3 |
frame_step = 15
|
4 |
resolution = 224
|
5 |
frame_size = (resolution, resolution)
|
@@ -26,7 +26,7 @@ class Config:
|
|
26 |
num_classes = len(id_to_name)
|
27 |
input_shape = (1, num_frames, resolution, resolution, 3)
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
classify_action_num_frames =
|
32 |
yield_frame_steps = 5
|
|
|
1 |
class Config:
|
2 |
+
num_frames = 4
|
3 |
frame_step = 15
|
4 |
resolution = 224
|
5 |
frame_size = (resolution, resolution)
|
|
|
26 |
num_classes = len(id_to_name)
|
27 |
input_shape = (1, num_frames, resolution, resolution, 3)
|
28 |
|
29 |
+
detect_object_frame_step = 5
|
30 |
+
classify_action_frame_step = frame_step
|
31 |
+
classify_action_num_frames = num_frames
|
32 |
yield_frame_steps = 5
|
inference.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
from imgviz import instances2rgb
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
|
|
4 |
|
5 |
from configuration import Config
|
6 |
|
@@ -18,8 +18,7 @@ def detect_object(detector, frame):
|
|
18 |
boxes = result.boxes.xyxy.numpy()
|
19 |
detections = (
|
20 |
[result.names[i].capitalize() for i in classes],
|
21 |
-
boxes
|
22 |
-
)
|
23 |
return detections
|
24 |
|
25 |
def classify_action(classifier, frames, id_to_name):
|
@@ -48,12 +47,10 @@ def draw_boxes(frame, detections, actions):
|
|
48 |
line_width = 2
|
49 |
captions = [
|
50 |
f'{class_name}\n' + '\n'.join(actions if i == max_area_id else [])
|
51 |
-
for (i, class_name) in enumerate(classes)
|
52 |
-
]
|
53 |
bboxes = [
|
54 |
[box[1], box[0], box[3], box[2]]
|
55 |
-
for box in boxes
|
56 |
-
]
|
57 |
frame = instances2rgb(
|
58 |
frame,
|
59 |
labels=labels,
|
@@ -61,8 +58,7 @@ def draw_boxes(frame, detections, actions):
|
|
61 |
bboxes=bboxes,
|
62 |
colormap=colormap,
|
63 |
font_size=20,
|
64 |
-
line_width=line_width
|
65 |
-
)
|
66 |
return frame
|
67 |
|
68 |
def FrameProcessor(detector, classifier, config: Config):
|
|
|
|
|
1 |
import tensorflow as tf
|
2 |
import numpy as np
|
3 |
+
from imgviz import instances2rgb
|
4 |
|
5 |
from configuration import Config
|
6 |
|
|
|
18 |
boxes = result.boxes.xyxy.numpy()
|
19 |
detections = (
|
20 |
[result.names[i].capitalize() for i in classes],
|
21 |
+
boxes)
|
|
|
22 |
return detections
|
23 |
|
24 |
def classify_action(classifier, frames, id_to_name):
|
|
|
47 |
line_width = 2
|
48 |
captions = [
|
49 |
f'{class_name}\n' + '\n'.join(actions if i == max_area_id else [])
|
50 |
+
for (i, class_name) in enumerate(classes)]
|
|
|
51 |
bboxes = [
|
52 |
[box[1], box[0], box[3], box[2]]
|
53 |
+
for box in boxes]
|
|
|
54 |
frame = instances2rgb(
|
55 |
frame,
|
56 |
labels=labels,
|
|
|
58 |
bboxes=bboxes,
|
59 |
colormap=colormap,
|
60 |
font_size=20,
|
61 |
+
line_width=line_width)
|
|
|
62 |
return frame
|
63 |
|
64 |
def FrameProcessor(detector, classifier, config: Config):
|
model.py
CHANGED
@@ -48,7 +48,3 @@ def load_classifier(config: Config):
|
|
48 |
|
49 |
def load_detector(config: Config):
|
50 |
return YOLO(config.detector_path)
|
51 |
-
|
52 |
-
def compile_classifier(model, config: Config):
|
53 |
-
optimizer = keras.optimizers.Adam(learning_rate=config.learning_rate)
|
54 |
-
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
|
|
48 |
|
49 |
def load_detector(config: Config):
|
50 |
return YOLO(config.detector_path)
|
|
|
|
|
|
|
|