Spaces:
Sleeping
Sleeping
Changing to different streaming demo
Browse files
app.py
CHANGED
@@ -1,59 +1,51 @@
|
|
1 |
-
import cv2
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
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 |
-
live=True,
|
51 |
-
description="Real-Time Object Detection with YOLO and Gradio"
|
52 |
-
)
|
53 |
-
|
54 |
-
# Launch Gradio app
|
55 |
-
if __name__ == "__main__":
|
56 |
-
webcam_interface.launch()
|
57 |
|
58 |
|
59 |
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
import cv2
|
4 |
+
|
5 |
+
def transform_cv2(frame, transform):
|
6 |
+
if transform == "cartoon":
|
7 |
+
# prepare color
|
8 |
+
img_color = cv2.pyrDown(cv2.pyrDown(frame))
|
9 |
+
for _ in range(6):
|
10 |
+
img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
|
11 |
+
img_color = cv2.pyrUp(cv2.pyrUp(img_color))
|
12 |
+
|
13 |
+
# prepare edges
|
14 |
+
img_edges = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
15 |
+
img_edges = cv2.adaptiveThreshold(
|
16 |
+
cv2.medianBlur(img_edges, 7),
|
17 |
+
255,
|
18 |
+
cv2.ADAPTIVE_THRESH_MEAN_C,
|
19 |
+
cv2.THRESH_BINARY,
|
20 |
+
9,
|
21 |
+
2,
|
22 |
+
)
|
23 |
+
img_edges = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2RGB)
|
24 |
+
# combine color and edges
|
25 |
+
img = cv2.bitwise_and(img_color, img_edges)
|
26 |
+
return img
|
27 |
+
elif transform == "edges":
|
28 |
+
# perform edge detection
|
29 |
+
img = cv2.cvtColor(cv2.Canny(frame, 100, 200), cv2.COLOR_GRAY2BGR)
|
30 |
+
return img
|
31 |
+
else:
|
32 |
+
return np.flipud(frame)
|
33 |
+
|
34 |
+
|
35 |
+
css=""".my-group {max-width: 500px !important; max-height: 500px !important;}
|
36 |
+
.my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
|
37 |
+
|
38 |
+
with gr.Blocks(css=css) as demo:
|
39 |
+
with gr.Column(elem_classes=["my-column"]):
|
40 |
+
with gr.Group(elem_classes=["my-group"]):
|
41 |
+
transform = gr.Dropdown(choices=["cartoon", "edges", "flip"],
|
42 |
+
value="flip", label="Transformation")
|
43 |
+
input_img = gr.Image(sources=["webcam"], type="numpy", streaming=True)
|
44 |
+
input_img.stream(transform_cv2, [input_img, transform], [input_img], time_limit=30, stream_every=0.1)
|
45 |
+
|
46 |
+
|
47 |
+
demo.launch()
|
48 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
|
51 |
|