brainai-spaces commited on
Commit
557520a
Β·
verified Β·
1 Parent(s): 9ebc5b3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import utils
3
+ import cv2
4
+ import numpy
5
+ import io
6
+ import tempfile
7
+ from PIL import Image
8
+ import moviepy.editor as mpy
9
+ from camera_input_live import camera_input_live
10
+
11
+ st.set_page_config(
12
+ page_title="Hello Text Detection",
13
+ page_icon=":sun_with_face:",
14
+ layout="centered",
15
+ initial_sidebar_state="expanded",)
16
+
17
+ st.title("Hello Text Dection :sun_with_face:")
18
+
19
+ st.sidebar.header("Type")
20
+ source_radio = st.sidebar.radio("Select Source", ["IMAGE", "VIDEO", "WEBCAM"])
21
+
22
+ st.sidebar.header("Confidence")
23
+ conf_threshold = float(st.sidebar.slider("Select the Confidence Threshold", 10, 100, 20))/100
24
+
25
+ input = None
26
+ if source_radio == "IMAGE":
27
+ st.sidebar.header("Upload")
28
+ input = st.sidebar.file_uploader("Choose an image.", type=("jpg", "png"))
29
+ if input is not None:
30
+ uploaded_image = Image.open(input)
31
+ uploaded_image_cv = cv2.cvtColor(numpy.array(uploaded_image), cv2.COLOR_RGB2BGR)
32
+ boxes, resized_image = utils.predict_image(uploaded_image_cv, conf_threshold = conf_threshold)
33
+ result_image = utils.convert_result_to_image(uploaded_image_cv, resized_image, boxes, conf_labels=False)
34
+ st.image(result_image, channels = "RGB")
35
+ st.markdown(f"<h4 style='color: blue;'><strong>The result of running the AI inference on an image.</strong></h4>", unsafe_allow_html=True)
36
+ else:
37
+ st.image("data/intel_rnb.jpg")
38
+ st.write("Click on 'Browse Files' in the sidebar to run inference on an image." )
39
+
40
+ def play_video(video_source):
41
+ camera = cv2.VideoCapture(video_source)
42
+ fps = camera.get(cv2.CAP_PROP_FPS)
43
+ temp_file_2 = tempfile.NamedTemporaryFile(delete=False,suffix='.mp4')
44
+ video_row=[]
45
+ # frame
46
+ total_frames = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
47
+ progress_bar = st.progress(0)
48
+ frame_count = 0
49
+ st_frame = st.empty()
50
+ while(camera.isOpened()):
51
+ ret, frame = camera.read()
52
+ if ret:
53
+ try:
54
+ boxes, resized_image = utils.predict_image(frame, conf_threshold)
55
+ visualized_image = utils.convert_result_to_image(frame, resized_image, boxes, conf_labels=False)
56
+ except:
57
+ visualized_image = frame
58
+ st_frame.image(visualized_image, channels = "BGR")
59
+ video_row.append(cv2.cvtColor(visualized_image,cv2.COLOR_BGR2RGB))
60
+ frame_count +=1
61
+ progress_bar.progress(frame_count/total_frames, text=None)
62
+
63
+ else:
64
+ progress_bar.empty()
65
+ camera.release()
66
+ st_frame.empty()
67
+ break
68
+ clip = mpy.ImageSequenceClip(video_row, fps = fps)
69
+ clip.write_videofile(temp_file_2.name)
70
+ st.video(temp_file_2.name)
71
+
72
+ # 파일 μ—…λ‘œλ“œ 처리
73
+ temporary_location = None
74
+ if source_radio == "VIDEO":
75
+ st.sidebar.header("Upload")
76
+ input_file = st.sidebar.file_uploader("Choose a video.", type=("mp4"))
77
+ if input_file is not None:
78
+ # νŒŒμΌμ„ μž„μ‹œ κ²½λ‘œμ— μ €μž₯
79
+ g = io.BytesIO(input_file.read())
80
+ temporary_location = "upload.mp4"
81
+ with open(temporary_location, "wb") as out:
82
+ out.write(g.read())
83
+ out.close()
84
+ # μ—…λ‘œλ“œλœ λΉ„λ””μ˜€ 파일이 μžˆλŠ” 경우 λΉ„λ””μ˜€ μž¬μƒ
85
+ if temporary_location is not None:
86
+ play_video(temporary_location)
87
+ else:
88
+ st.video("data/sample_video.mp4")
89
+ st.write("Click on 'Browse Files' in the sidebar to run inference on a video.")
90
+
91
+ if source_radio == "WEBCAM":
92
+ input = camera_input_live()
93
+ uploaded_image = Image.open(input)
94
+ uploaded_image_cv = cv2.cvtColor(numpy.array(uploaded_image), cv2.COLOR_RGB2BGR)
95
+ boxes, resized_image = utils.predict_image(uploaded_image_cv, conf_threshold)
96
+ visualized_image = utils.convert_result_to_image(uploaded_image_cv, resized_image, boxes, conf_labels=False)
97
+ st.image(visualized_image, channels = "RGB")