File size: 4,077 Bytes
557520a b5a6688 557520a afbd1bc 557520a afbd1bc 557520a afbd1bc 557520a afbd1bc 557520a afbd1bc d4547ab 557520a 77c2e7d 557520a d4547ab 557520a 98b97f0 c460043 557520a d4547ab 557520a 98b97f0 c460043 557520a b60fdd3 557520a | 1 2 3 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | import streamlit as st
import utils
import cv2
import numpy as np
import io
import tempfile
from PIL import Image
import moviepy.editor as mpy
from camera_input_live import camera_input_live
st.set_page_config(
page_title="κΈμ μΈμ AI μμ€ν
",
page_icon=":sun_with_face:",
layout="centered",
initial_sidebar_state="expanded",)
st.title("κΈμ μΈμ AI μμ€ν
:sun_with_face:")
st.sidebar.header("λ©λ΄")
source_radio = st.sidebar.radio("μ ννμΈμ", ["IMAGE", "VIDEO", "WEBCAM"])
st.sidebar.header("μ λ’°λ")
conf_threshold = float(st.sidebar.slider("μ λ’°λ μκ³κ°μ μ ννμΈμ", 10, 100, 20))/100
input = None
if source_radio == "IMAGE":
st.sidebar.header("μ΄λ―Έμ§ νμΌ μ
λ‘λ")
input = st.sidebar.file_uploader("μ΄λ―Έμ§ νμΌμ μ ννμΈμ.", type=("jpg", "png"))
if input is not None:
uploaded_image = Image.open(input)
uploaded_image_cv = cv2.cvtColor(np.array(uploaded_image), cv2.COLOR_RGB2BGR)
boxes, resized_image = utils.predict_image(uploaded_image_cv, conf_threshold = conf_threshold)
result_image = utils.convert_result_to_image(uploaded_image_cv, resized_image, boxes, conf_labels=False)
st.image(result_image, channels = "RGB")
st.markdown(f"<h4 style='color: blue;'><strong>μ΄λ―Έμ§μμ AI μΆλ‘ μ μ€νν κ²°κ³Ό μ
λλ€.</strong></h4>", unsafe_allow_html=True)
else:
st.write("μΌμͺ½ λ©λ΄ 'Browse files' λ²νΌμ ν΄λ¦νμ¬ μ΄λ―Έμ§ νμΌμ μ ννλ©΄ AI μΆλ‘ μ΄ μμλ©λλ€." )
st.image("data/intel_rnb.jpg")
def play_video(video_source):
camera = cv2.VideoCapture(video_source)
fps = camera.get(cv2.CAP_PROP_FPS)
temp_file_2 = tempfile.NamedTemporaryFile(delete=False,suffix='.mp4')
video_row=[]
# frame
total_frames = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
progress_bar = st.progress(0)
frame_count = 0
st_frame = st.empty()
while(camera.isOpened()):
ret, frame = camera.read()
if ret:
try:
boxes, resized_image = utils.predict_image(frame, conf_threshold)
visualized_image = utils.convert_result_to_image(frame, resized_image, boxes, conf_labels=False)
except:
visualized_image = frame
st_frame.image(visualized_image, channels = "BGR")
video_row.append(cv2.cvtColor(visualized_image,cv2.COLOR_BGR2RGB))
frame_count +=1
progress_bar.progress(frame_count/total_frames, text=None)
else:
progress_bar.empty()
camera.release()
st_frame.empty()
break
clip = mpy.ImageSequenceClip(video_row, fps = fps)
clip.write_videofile(temp_file_2.name)
st.video(temp_file_2.name)
# νμΌ μ
λ‘λ μ²λ¦¬
temporary_location = None
if source_radio == "VIDEO":
st.sidebar.header("λΉλμ€ νμΌ μ
λ‘λ")
input_file = st.sidebar.file_uploader("λΉλμ€ νμΌμ μ ννμΈμ.", type=("mp4"))
if input_file is not None:
# νμΌμ μμ κ²½λ‘μ μ μ₯
g = io.BytesIO(input_file.read())
temporary_location = "upload.mp4"
with open(temporary_location, "wb") as out:
out.write(g.read())
out.close()
# μ
λ‘λλ λΉλμ€ νμΌμ΄ μλ κ²½μ° λΉλμ€ μ¬μ
if temporary_location is not None:
play_video(temporary_location)
else:
st.write("μΌμͺ½ λ©λ΄ 'Browse files' λ²νΌμ ν΄λ¦νμ¬ μμ νμΌμ μ ννλ©΄ AI μΆλ‘ μ΄ μμλ©λλ€.")
st.video("data/sample_video.mp4")
if source_radio == "WEBCAM":
input = camera_input_live()
uploaded_image = Image.open(input)
uploaded_image_cv = cv2.cvtColor(np.array(uploaded_image), cv2.COLOR_RGB2BGR)
boxes, resized_image = utils.predict_image(uploaded_image_cv, conf_threshold)
visualized_image = utils.convert_result_to_image(uploaded_image_cv, resized_image, boxes, conf_labels=False)
st.image(visualized_image, channels = "RGB") |