Spaces:
Runtime error
Runtime error
Geraldine J
commited on
Commit
•
6059241
1
Parent(s):
cbd2407
Update app aws
Browse files
app.py
CHANGED
@@ -29,31 +29,70 @@ Este Notebook se acelera opcionalmente con un entorno de ejecución de GPU
|
|
29 |
# Código
|
30 |
"""
|
31 |
|
32 |
-
#!pip install -qr https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt gradio # install dependencies
|
33 |
-
|
34 |
import gradio as gr
|
35 |
import pandas as pd
|
36 |
import torch
|
37 |
import logging
|
38 |
import json
|
39 |
import re
|
|
|
|
|
|
|
|
|
|
|
40 |
from PIL import Image
|
41 |
-
|
42 |
# Images
|
43 |
torch.hub.download_url_to_file('https://i.pinimg.com/564x/18/0b/00/180b00e454362ff5caabe87d9a763a6f.jpg', 'ejemplo1.jpg')
|
44 |
torch.hub.download_url_to_file('https://i.pinimg.com/564x/3b/2f/d4/3b2fd4b6881b64429f208c5f32e5e4be.jpg', 'ejemplo2.jpg')
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
def removeStr(string):
|
54 |
return string.replace(" ", "")
|
55 |
|
56 |
-
|
|
|
|
|
|
|
57 |
x = re.findall("obo mar", d)
|
58 |
y = re.findall("elica", d)
|
59 |
z = re.findall("elica", f)
|
@@ -70,7 +109,7 @@ def listJSON(a,b,c,d,e,f):
|
|
70 |
strlista = '"detail":[{"quantity":"'+str(removeStr(c))+'","description":"'+str(d)+'"}]'
|
71 |
else:
|
72 |
strlista = '"detail":[{"quantity":"'+str(removeStr(c))+'","description":"'+str(d)+'"},{"quantity":"'+str(removeStr(e))+'","description":"'+str(f)+'"}]'
|
73 |
-
strlist = '{"image":"'+str(removeStr(a))+'","size":"'+str(removeStr(b))+'",'+strlista+'}'
|
74 |
json_string = json.loads(strlist)
|
75 |
return json_string
|
76 |
|
@@ -98,22 +137,31 @@ def arrayLista(a,b,c,d):
|
|
98 |
|
99 |
def yolo(size, iou, conf, im):
|
100 |
try:
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
except Exception as e:
|
116 |
-
|
117 |
|
118 |
#------------ Interface-------------
|
119 |
|
|
|
29 |
# Código
|
30 |
"""
|
31 |
|
|
|
|
|
32 |
import gradio as gr
|
33 |
import pandas as pd
|
34 |
import torch
|
35 |
import logging
|
36 |
import json
|
37 |
import re
|
38 |
+
import os
|
39 |
+
import boto3
|
40 |
+
from botocore.exceptions import NoCredentialsError
|
41 |
+
import tempfile
|
42 |
+
import io
|
43 |
from PIL import Image
|
|
|
44 |
# Images
|
45 |
torch.hub.download_url_to_file('https://i.pinimg.com/564x/18/0b/00/180b00e454362ff5caabe87d9a763a6f.jpg', 'ejemplo1.jpg')
|
46 |
torch.hub.download_url_to_file('https://i.pinimg.com/564x/3b/2f/d4/3b2fd4b6881b64429f208c5f32e5e4be.jpg', 'ejemplo2.jpg')
|
47 |
|
48 |
+
# Envio de imagenes a S3
|
49 |
+
def upload_file(file_name, bucket=None, object_name=None):
|
50 |
+
"""Upload a file to an S3 bucket
|
51 |
+
|
52 |
+
:param file_name: File to upload
|
53 |
+
:param bucket: Bucket to upload to
|
54 |
+
:param object_name: S3 object name. If not specified then file_name is used
|
55 |
+
:return: Json if file was uploaded, else False
|
56 |
+
"""
|
57 |
+
|
58 |
+
# If S3 object_name was not specified, use file_name
|
59 |
+
if object_name is None:
|
60 |
+
object_name = os.path.basename(file_name+".jpg")
|
61 |
+
if bucket is None:
|
62 |
+
bucket = 'oceanapp'
|
63 |
+
s3_client = boto3.client('s3', os.environ['aws_access_key_id'],os.environ['aws_secret_access_key'],os.environ['region'])
|
64 |
+
aws_region = boto3.session.Session().region_name
|
65 |
+
# Upload the file
|
66 |
+
try:
|
67 |
+
with open(file_name, "rb") as f:
|
68 |
+
response = s3_client.upload_fileobj(f, bucket, object_name)
|
69 |
+
s3_url = f"https://{bucket}.s3.{aws_region}.amazonaws.com/{object_name}"
|
70 |
+
stado = '"url_details":[{"statusCode":200, "s3_url":"'+s3_url+'"}]'
|
71 |
+
print(s3_url)
|
72 |
+
except FileNotFoundError:
|
73 |
+
print("The file was not found")
|
74 |
+
return False
|
75 |
+
except NoCredentialsError as e:
|
76 |
+
logging.error(e)
|
77 |
+
return False
|
78 |
+
return stado
|
79 |
+
|
80 |
+
#Imagen temporal guardada en upload_file
|
81 |
+
def tempFileJSON(img_file):
|
82 |
+
temp = tempfile.NamedTemporaryFile(mode="wb")
|
83 |
+
with temp as jpg:
|
84 |
+
jpg.write(img_file)
|
85 |
+
print(jpg.name)
|
86 |
+
uf = upload_file(jpg.name)
|
87 |
+
return uf
|
88 |
|
89 |
def removeStr(string):
|
90 |
return string.replace(" ", "")
|
91 |
|
92 |
+
# Model
|
93 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True, autoshape=True) # local model o google colab
|
94 |
+
|
95 |
+
def listJSON(a,b,c,d,e,f,resImg):
|
96 |
x = re.findall("obo mar", d)
|
97 |
y = re.findall("elica", d)
|
98 |
z = re.findall("elica", f)
|
|
|
109 |
strlista = '"detail":[{"quantity":"'+str(removeStr(c))+'","description":"'+str(d)+'"}]'
|
110 |
else:
|
111 |
strlista = '"detail":[{"quantity":"'+str(removeStr(c))+'","description":"'+str(d)+'"},{"quantity":"'+str(removeStr(e))+'","description":"'+str(f)+'"}]'
|
112 |
+
strlist = '{"image":"'+str(removeStr(a))+'","size":"'+str(removeStr(b))+'",'+strlista+','+resImg+'}'
|
113 |
json_string = json.loads(strlist)
|
114 |
return json_string
|
115 |
|
|
|
137 |
|
138 |
def yolo(size, iou, conf, im):
|
139 |
try:
|
140 |
+
'''Wrapper fn for gradio'''
|
141 |
+
# gain
|
142 |
+
g = (int(size) / max(im.size))
|
143 |
+
# resize
|
144 |
+
im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS)
|
145 |
+
|
146 |
+
model.iou = iou
|
147 |
+
|
148 |
+
model.conf = conf
|
149 |
+
# inference
|
150 |
+
results2 = model(im)
|
151 |
+
# updates results.imgs with boxes and labels
|
152 |
+
results2.render()
|
153 |
+
results3 = str(results2)
|
154 |
+
#Transformando la img en bytes
|
155 |
+
pil_im = Image.fromarray(results2.ims[0])
|
156 |
+
b = io.BytesIO()
|
157 |
+
pil_im.save(b, 'jpeg')
|
158 |
+
im_bytes = b.getvalue()
|
159 |
+
fileImg = tempFileJSON(im_bytes)
|
160 |
+
lista = listJSON(results3[0:9], results3[11:18] ,results3[19:21],results3[22:32], results3[34:36], results3[36:45], fileImg)
|
161 |
+
lista2 = arrayLista(results3[19:21],results3[22:32], results3[34:36], results3[37:45])
|
162 |
+
return Image.fromarray(results2.ims[0]), lista2, lista
|
163 |
except Exception as e:
|
164 |
+
logging.error(e, exc_info=True)
|
165 |
|
166 |
#------------ Interface-------------
|
167 |
|