File size: 6,895 Bytes
fa6f892
 
dcda1d1
b60db7e
0d604bc
 
 
 
 
 
 
 
462bb22
9fdd6f8
fb370ac
c8b0e99
0d604bc
 
 
644a399
0d604bc
 
 
 
 
 
 
 
 
 
 
 
e88ad6a
0d604bc
ab26cd9
0d604bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
832c49c
0d604bc
d3b2c1d
832c49c
 
d3b2c1d
 
0d604bc
 
 
 
 
 
fa6f892
0d604bc
 
 
 
 
 
 
 
 
c1777d4
 
1fcad52
c1777d4
 
 
ab26cd9
bd22bb0
 
 
2ef0d6c
b276f38
bd22bb0
 
 
 
 
b276f38
7a51791
 
29c4264
bf3e5b9
 
ab26cd9
 
 
 
c1777d4
 
 
97e475b
 
 
 
 
 
33d89ab
97e475b
33d89ab
97e475b
 
33d89ab
2534654
97e475b
 
7777454
97e475b
 
 
 
 
 
 
f501bc2
97e475b
 
87095c4
aa7c965
7777454
251ff9c
87095c4
251ff9c
 
 
87095c4
 
29c4264
87095c4
 
 
 
251ff9c
29c4264
7777454
29c4264
 
 
 
 
 
 
 
 
 
 
 
 
251ff9c
260d3d5
97e475b
0d604bc
 
644a399
0426f21
c1777d4
 
97e475b
 
 
644a399
c1777d4
97e475b
5108c70
2dafcb9
aa7c965
9fdd6f8
644a399
 
d3b2c1d
0426f21
0d604bc
 
 
 
 
 
 
 
49a40ea
8d4e784
49a40ea
8d4e784
 
 
 
94b9996
f529a18
94b9996
 
08f5c27
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#https://streamlit-webrtc-example.herokuapp.com/
#https://github.com/amineHY/WebApp-Computer-Vision-streamlit
 
import streamlit as st
import os
from os import listdir
import wget
from PIL import Image
import io
import numpy as np
import cv2
import itertools
import sys
from deepface import DeepFace

  
def load_model():
    wpath = 'test_detection/yolov5/weights/crowdhuman_yolov5m.pt'
    if not os.path.exists(wpath):

        os.system("python -m pip install numpy torch pandas Pillow opencv-python-headless PyYAML>=5.3.1 torchvision>=0.8.1 matplotlib seaborn>=0.11.0 easydict")
        with st.spinner('Downloading model weights for crowdhuman_yolov5m'):
            os.system('wget -nc https://github.com/mikel-brostrom/Yolov5_DeepSort_Pytorch/releases/download/v.2.0/crowdhuman_yolov5m.pt -O test_detection/yolov5/weights/crowdhuman_yolov5m.pt')
    else:
        print("Model is here.")
        
        
        
#===================================================================================

# Ft saving uploaded video to directory
def save_uploaded_vid(uploadedfile):
    with open(os.path.join("test_detection/data", uploadedfile.name),"wb") as f:
        f.write(uploadedfile.getbuffer())
    return #st.success("Video saved in data dir ")

#@st.cache(ttl=3600, max_entries=10)
def load_output_video(vid):
    if isinstance(vid, str): 
        video = open(vid, 'rb')
    else: 
        video = vid.read()
        vname = vid.name
        save_uploaded_vid(vid)
    return video

def starter():
    st.image('test_detection/data/LOGOGlob.png', width = 400)
    st.title("Test of Person detection")
    st.text("")
    st.text("")
    #st.success("Welcome! Please upload a video!")
 
    args = { 'ParisManif' : 'ParisManif.mp4' }
    vid_upload  = st.file_uploader(label= 'Welcome! Please upload a video! ', type = ['mp4', 'avi'])
    
    vid_open = "test_detection/data/"+args['ParisManif'] if vid_upload is None else vid_upload
    vname = args['ParisManif'] if vid_upload is None else vid_upload.name
  
    video = load_output_video(vid_open)
    
                
    st.video(video) 
    
   
    vidcap = cv2.VideoCapture( "test_detection/data/"+vname) 
    #frames = cv.get_frames("data/"+vname)
    success, frame0 = vidcap.read()
    frame0 = cv2.cvtColor(frame0, cv2.COLOR_BGR2RGB)

    return vname, frame0
    
#===================================================================================    
    
def prediction(vname):
    
    
    vpath='test_detection/data/'+vname
    wpath = 'test_detection/yolov5/weights/crowdhuman_yolov5m.pt'
    if os.path.exists(wpath):
        with st.spinner('Running detection...'):
        
            st.write('vpath befor calling track : ', vpath)
            
            os.system("python test_detection/track.py --yolo_weights test_detection/yolov5/weights/crowdhuman_yolov5m.pt --img 352 --save-vid --save-txt --classes 1 --conf-thres 0.4 --source " + vpath)
            
            path = 'inference/output/'
            #st.write(os.path.exists(path), os.path.exists('inference/output/output'+vname))
            
            
            st.write(os.listdir('test_detection/'))
            
            st.write(os.listdir(path))
            
        with st.spinner('Video loading...'):
            os.system("ffmpeg -i inference/output/"+vname + " -vcodec libx264 -y inference/output/output_"+vname)
            path = 'inference/output/output_'+vname
            if os.path.exists(path):
                video_file = open(path, 'rb')
                video_bytes = video_file.read()
                st.video(video_bytes)
            
#===================================================================================                
            


def extract_heads(filepath, frame0):
    nbperson = 0
    listhead = []
    if os.path.exists(filepath):
        #st.write("filepath : ", filepath)
        array_from_file = np.loadtxt(filepath, dtype=int)
        #st.write('np of array load : ', array_from_file.shape)
        array_from_file = np.delete(array_from_file,np.s_[7:10], axis=1)
        nbperson = np.unique(array_from_file[:,1]).shape[0]
        st.subheader('Display some detected heads :')
        st.write(' Number of detected heads : ', nbperson )

        rows = 5
        cols = 5
        nbheads = rows*cols
        frame = frame0
        cont = array_from_file
        for a in range(nbheads):
            numh = a
            head = frame[cont[numh][3]:cont[numh][3]+cont[numh][5],cont[numh][2]:cont[numh][2]+cont[numh][4],:]
            listhead.append(head)
        #st.write('Len of liste heads : ', len(listhead))
    return nbperson, listhead

#*********************************************************************************************
def display_heads_(nbperson, listhead):
    rows_ = 5
    cols_ = 5
    k = 0
    for i in range(1, rows_):
        cols = st.columns(cols_)
        for j in range(1, cols_):
            k = k + 1
            cols[j].header("Person " + str(k))
            cols[j].image(listhead[k], use_column_width=True, width=150)
    return 

#*********************************************************************************************


def display_heads_analysis(nbperson, listhead):
    rows_ = 5
    cols_ = 5
    k = 0
    for i in range(1, rows_):
        cols = st.columns(cols_)
        for j in range(1, cols_):
            k = k + 1
            cols[j].header("Person " + str(k))
            
            obj = DeepFace.analyze(img_path = listhead[k], actions = ['age', 'gender'], enforce_detection=False)
            #capt = 'age : '+ obj['age'] + ', gender : ' + obj['gender']
            capt = 'age : '+ str(obj['age']) + ', gender : ' + str(obj['gender'])
            cols[j].image(listhead[k], use_column_width=True, width=150, caption = capt )
    return 

 
#===================================================================================                            
def main():
    vname, frame0 = starter()
    
    nbperson = 0
    if st.button('Heads detection!'):
        prediction(vname)
        
        filepath = 'inference/output/'+vname
        filepath = filepath[:-3]+'txt'
        
        st.success("Click again to retry or try a different video by uploading")
        nbperson, listhead = extract_heads(filepath, frame0) 
        
   
        display_heads_(nbperson, listhead)
        
        #st.subheader('Continue to face analysis !')
        #display_heads_analysis(nbperson, listhead)

 
    return   
        
                        
        
        

if __name__ == '__main__':

  os.system('git clone --recurse-submodules https://github.com/nnassime/test_detection.git')
  
  load_model()
  
  st.write(os.listdir('test_detection/'))
  st.write(os.listdir('test_detection/yolov5/weights/'))
  
  st.write(os.listdir('test_detection/deep_sort_pytorch/deep_sort/deep/checkpoint/'))
 

  
  main()