-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
80 lines (57 loc) · 2.01 KB
/
app.py
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
from flask import Flask, jsonify, make_response, request
import requests
from flask_pymongo import PyMongo
from function.to_frame import *
from collections import OrderedDict
from detect import *
from function.s3_control import *
import ffmpeg
import shutil
import asyncio
app = Flask(__name__)
app.config["MONGO_URI"] = os.environ['MONGO_URI_env']
mongodb_client = PyMongo(app)
coll = mongodb_client.db.video_files_list
coll2 = mongodb_client.db.images_coll
async def to_s3(video_pk, dir):
list_dir = []
list_dir = os.listdir(dir)
count = 0
output2=OrderedDict()
image_list=[]
for filename in list_dir:
# upload local image files to gcp storage
upload_blob_file(dir + '/'+ filename, 'images/'+ str(video_pk)+ '/'+ filename)
#url form
img_path = 'https://crayon-team-j.s3.ap-northeast-2.amazonaws.com/images/'+str(video_pk)+'/'+filename
image_list.append({'time':count, 'path':img_path})
count+=1
output2["video_pk"]=video_pk
output2["image_list"]=image_list
coll2.insert(output2)
async def run_yolo(req_data, video_pk, video_path):
req_data = request.json
video_pk = req_data['video_pk']
video_path = req_data['video_path']
dir = 'data/'+str(video_pk)
os.makedirs(dir)
output_name = dir+'/image-%3d.jpg'
video_to_Img(video_path, video_pk, output_name)
#run Yolo
result = await run(source=dir)
#save result to mongoDB
coll.insert({'video_number':video_pk, 'detection_list':result})
#save images to S3
await to_s3(video_pk, dir)
# delete local directory
shutil.rmtree(dir)
@app.route('/to_yolo', methods = ['POST'])
def run_yolo1():
req_data = request.json
video_pk = req_data['video_pk']
video_path = req_data['video_path']
asyncio.run(run_yolo(req_data, video_pk, video_path))
return make_response(jsonify({'Result': 'Success', 'video_pk': video_pk}), 200)
if __name__ == '__main__':
app.run(debug=True)
#flask run --host=0.0.0.0 --port=5001