|
| 1 | +from email.mime import image |
| 2 | +from re import S |
| 3 | +from django.db import connection |
| 4 | +import streamlit as st |
| 5 | +import mediapipe as mp |
| 6 | +import cv2 |
| 7 | +import numpy as np |
| 8 | +import tempfile |
| 9 | +import time |
| 10 | +from PIL import Image |
| 11 | + |
| 12 | +mp_drawing = mp.solutions.drawing_utils |
| 13 | +mp_face_mesh = mp.solutions.face_mesh |
| 14 | + |
| 15 | +DEMO_IMAGE = 'demo_image.jpg' |
| 16 | +DEMO_VIDEO = 'video.mp4' |
| 17 | + |
| 18 | +st.title('Face Mesh App') |
| 19 | + |
| 20 | +st.markdown( |
| 21 | + """ |
| 22 | + <style> |
| 23 | + [data-testid="stSidebar"][aria-expanded="true"] > div:first-child{ |
| 24 | + width:350px |
| 25 | + } |
| 26 | + [data-testid="stSidebar"][aria-expanded="false"] > div:first-child{ |
| 27 | + width:350px |
| 28 | + margin-left: -350px |
| 29 | + } |
| 30 | + </style> |
| 31 | + """, |
| 32 | + unsafe_allow_html=True, |
| 33 | +) |
| 34 | + |
| 35 | +st.sidebar.title("App Sidebar") |
| 36 | +st.sidebar.subheader('parameters') |
| 37 | + |
| 38 | + |
| 39 | +@st.cache() |
| 40 | +def image_resize(image, width: None, height: None, inter=cv2.INTER_AREA): |
| 41 | + dim = None |
| 42 | + (h, w) = image.shape[:2] |
| 43 | + |
| 44 | + if width is None and height is None: |
| 45 | + return image |
| 46 | + |
| 47 | + if width is None: |
| 48 | + r = width/float(w) |
| 49 | + dim = (int(w*r), height) |
| 50 | + |
| 51 | + else: |
| 52 | + r = width/float(w) |
| 53 | + dim = (width, int(h*r)) |
| 54 | + |
| 55 | + # resize the image |
| 56 | + resized = cv2.resize(image, dim, interpolation=inter) |
| 57 | + |
| 58 | + return resized |
| 59 | + |
| 60 | + |
| 61 | +app_mode = st.sidebar.selectbox('Choose the App mode', |
| 62 | + ['About App', 'Run on Image', 'Run on Video'] |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +if app_mode == 'About App': |
| 67 | + st.markdown( |
| 68 | + "In this application we are using **Mediapipe** for creating an App") |
| 69 | + st.markdown( |
| 70 | + """ |
| 71 | + <style> |
| 72 | + [data-testid="stSidebar"][aria-expanded="true"] > div:first-child{ |
| 73 | + width:350px |
| 74 | + } |
| 75 | + [data-testid="stSidebar"][aria-expanded="false"] > div:first-child{ |
| 76 | + width:350px |
| 77 | + margin-left: -350px |
| 78 | + } |
| 79 | + </style> |
| 80 | + """, |
| 81 | + unsafe_allow_html=True, |
| 82 | + ) |
| 83 | + st.video("https://youtu.be/TVWfKsFyiP0") |
| 84 | +elif app_mode == "Run on Image": |
| 85 | + drawing_spec = mp_drawing.DrawingSpec(thickness=2, circle_radius=1) |
| 86 | + |
| 87 | + st.sidebar.markdown("---") |
| 88 | + st.markdown( |
| 89 | + """ |
| 90 | + <style> |
| 91 | + [data-testid="stSidebar"][aria-expanded="true"] > div:first-child{ |
| 92 | + width:350px |
| 93 | + } |
| 94 | + [data-testid="stSidebar"][aria-expanded="false"] > div:first-child{ |
| 95 | + width:350px |
| 96 | + margin-left: -350px |
| 97 | + } |
| 98 | + </style> |
| 99 | + """, |
| 100 | + unsafe_allow_html=True, |
| 101 | + ) |
| 102 | + |
| 103 | + st.markdown("**Detected Faces**") |
| 104 | + kpil_text = st.markdown("0") |
| 105 | + |
| 106 | + max_faces = st.sidebar.number_input("Maximum Number of face", value=2, min_value=1) |
| 107 | + st.markdown("----") |
| 108 | + detection_confidence=st.sidebar.slider( |
| 109 | + "Min Detection Confidence", min_value=0.0, max_value=0.0, value=0.5) |
| 110 | + st.markdown("---") |
| 111 | + |
| 112 | + img_file_buffer=st.sidebar.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"]) |
| 113 | + if img_file_buffer is not None: |
| 114 | + image=np.array( |
| 115 | + Image.open(img_file_buffer)) |
| 116 | + else: |
| 117 | + demo_image=DEMO_IMAGE |
| 118 | + image=np.array(Image.open(demo_image)) |
| 119 | + |
| 120 | + st.sidebar.text("Original Image") |
| 121 | + st.sidebar.image(image) |
| 122 | + face_count=0 |
| 123 | + |
| 124 | + # Dashboard |
| 125 | + with mp_face_mesh.FaceMesh(static_image_mode=True,max_num_faces=max_faces, |
| 126 | + min_detection_confidence=detection_confidence) as face_mesh: |
| 127 | + results=face_mesh.process(image) |
| 128 | + out_image=image.copy() |
| 129 | + |
| 130 | + # face landmark drawing |
| 131 | + for face_landmarks in results.multi_face_landmarks: |
| 132 | + face_count += 1 |
| 133 | + |
| 134 | + mp_drawing.draw_landmarks( |
| 135 | + image = out_image, |
| 136 | + landmark_list=face_landmarks, |
| 137 | + connections= mp_face_mesh.FACE_CONNECTIONS, |
| 138 | + landmark_drawing_spec = drawing_spec) |
| 139 | + |
| 140 | + kpil_text.write(f"<h1 style='text-align:center; color:red;'>{face_count}</h1>",unsafe_allow_html=True) |
| 141 | + st.subheader('Output Image') |
| 142 | + st.image(out_image,use_column_width=True) |
| 143 | + |
| 144 | +elif app_mode == "Run on Video": |
| 145 | + |
| 146 | + st.set_option('deprecation.showfileUploaderEncoding',False) |
| 147 | + |
| 148 | + use_webcam = st.sidebar.button('Use Webcam') |
| 149 | + record = st.sidebar.checkbox('Record Video') |
| 150 | + |
| 151 | + if record: |
| 152 | + st.checkbox("Recording",value=True) |
| 153 | + |
| 154 | + st.markdown( |
| 155 | + """ |
| 156 | + <style> |
| 157 | + [data-testid="stSidebar"][aria-expanded="true"] > div:first-child{ |
| 158 | + width:350px |
| 159 | + } |
| 160 | + [data-testid="stSidebar"][aria-expanded="false"] > div:first-child{ |
| 161 | + width:350px |
| 162 | + margin-left: -350px |
| 163 | + } |
| 164 | + </style> |
| 165 | + """, |
| 166 | + unsafe_allow_html=True, |
| 167 | + ) |
| 168 | + |
| 169 | + |
| 170 | + max_faces = st.sidebar.number_input("Maximum Number of face", value=5, min_value=1) |
| 171 | + st.markdown("----") |
| 172 | + detection_confidence=st.sidebar.slider( |
| 173 | + "Min Detection Confidence", min_value=0.0, max_value=1.0, value=0.5) |
| 174 | + tracking_confidence = st.sidebar.slider( |
| 175 | + "Min Tracking Confidence", min_value=0.0, max_value=1.0, value=0.5) |
| 176 | + st.markdown("---") |
| 177 | + |
| 178 | + st.markdown("## Output") |
| 179 | + |
| 180 | + stframe = st.empty() |
| 181 | + video_file_buffer = st.sidebar.file_uploader("Upload a Video",type=['mp4','mov','avi','asf','m4v']) |
| 182 | + tffile = tempfile.NamedTemporaryFile(delete=False) |
| 183 | + |
| 184 | + |
| 185 | + ## Input Video |
| 186 | + |
| 187 | + if not video_file_buffer: |
| 188 | + if use_webcam: |
| 189 | + vid = cv2.VideoCapture(0) |
| 190 | + else: |
| 191 | + vid = cv2.VideoCapture(DEMO_VIDEO) |
| 192 | + tffile.name = DEMO_VIDEO |
| 193 | + else: |
| 194 | + tffile.write(video_file_buffer.read()) |
| 195 | + vid = cv2.VideoCapture(tffile.name) |
| 196 | + |
| 197 | + width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) |
| 198 | + height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
| 199 | + fps_input = int(vid.get(cv2.CAP_PROP_FPS)) |
| 200 | + |
| 201 | + # Recording part |
| 202 | + |
| 203 | + codec= cv2.VideoWriter_fourcc('M','J','P','G') |
| 204 | + out = cv2.VideoWriter('Output.mp4',codec,fps_input,(width,height)) |
| 205 | + |
| 206 | + st.sidebar.text("Input Video") |
| 207 | + st.sidebar.video(tffile.name) |
| 208 | + |
| 209 | + fps = 0 |
| 210 | + i = 0 |
| 211 | + |
| 212 | + drawing_spec = mp_drawing.DrawingSpec(thickness=2, circle_radius=1) |
| 213 | + |
| 214 | + kpi1,kpi2,kpi3 = st.beta_columns(3) |
| 215 | + |
| 216 | + with kpi1: |
| 217 | + st.markdown("**Frame Rate**") |
| 218 | + kpi1_text = st.markdown("0") |
| 219 | + |
| 220 | + with kpi2: |
| 221 | + st.markdown("**Detected Faces**") |
| 222 | + kpi2_text = st.markdown("0") |
| 223 | + |
| 224 | + with kpi3: |
| 225 | + st.markdown("**Image Width**") |
| 226 | + kpi3_text = st.markdown("0") |
| 227 | + |
| 228 | + st.markdown("<hr/>",unsafe_allow_html=True) |
| 229 | + |
| 230 | + # Predictor |
| 231 | + with mp_face_mesh.FaceMesh(static_image_mode=True,max_num_faces=max_faces, |
| 232 | + min_detection_confidence=detection_confidence, min_tracking_confidence=tracking_confidence) as face_mesh: |
| 233 | + prevTime = 0 |
| 234 | + |
| 235 | + while vid.isOpened(): |
| 236 | + i+=1 |
| 237 | + ret,frame= vid.read() |
| 238 | + if not ret: |
| 239 | + continue |
| 240 | + |
| 241 | + results = face_mesh.process(frame) |
| 242 | + frame.flags.writeable = True |
| 243 | + |
| 244 | + |
| 245 | + face_count = 0 |
| 246 | + if results.multi_face_landmarks: |
| 247 | + for face_landmarks in results.multi_face_landmarks: |
| 248 | + face_count += 1 |
| 249 | + |
| 250 | + mp_drawing.draw_landmarks( |
| 251 | + image=frame, |
| 252 | + landmark_list=face_landmarks, |
| 253 | + connections=mp_face_mesh.FACE_CONNECTIONS, |
| 254 | + landmark_drawing_spec=drawing_spec, |
| 255 | + connection_drawing_spec= drawing_spec) |
| 256 | + |
| 257 | + |
| 258 | + # FPS Counter Logic |
| 259 | + currTime = time.time() |
| 260 | + fps= 1/(currTime-prevTime) |
| 261 | + prevTime = currTime |
| 262 | + |
| 263 | + if record: |
| 264 | + out.write(frame) |
| 265 | + |
| 266 | + # Dashboard |
| 267 | + kpi1_text.write( |
| 268 | + f"<h1 style='text-align:center; color:red;'>{int(fps)}</h1>", unsafe_allow_html=True) |
| 269 | + kpi2_text.write( |
| 270 | + f"<h1 style='text-align:center; color:red;'>{face_count}</h1>", unsafe_allow_html=True) |
| 271 | + kpi3_text.write( |
| 272 | + f"<h1 style='text-align:center; color:red;'>{width}</h1>", unsafe_allow_html=True) |
| 273 | + |
| 274 | + frame = cv2.resize(frame,(0,0),fx=0.8,fy=0.8) |
| 275 | + frame = image_resize(image=frame,width=640,height=100) |
| 276 | + stframe.image(frame,channels='BGR',use_column_width=True) |
| 277 | + |
| 278 | + # results=face_mesh.process(image) |
| 279 | + # out_image=image.copy() |
| 280 | + |
| 281 | + # face landmark drawing |
| 282 | + |
| 283 | + st.subheader('Output Image') |
| 284 | + #st.image(frame,use_column_width=True) |
| 285 | + |
0 commit comments