Geraldine J commited on
Commit
b00b76d
1 Parent(s): fc237d8

Update file

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -42,7 +42,7 @@ import tempfile
42
  import io
43
  from PIL import Image
44
  from pandas import json_normalize
45
- # Images
46
  torch.hub.download_url_to_file('https://i.pinimg.com/564x/18/0b/00/180b00e454362ff5caabe87d9a763a6f.jpg', 'ejemplo1.jpg')
47
  torch.hub.download_url_to_file('https://i.pinimg.com/564x/3b/2f/d4/3b2fd4b6881b64429f208c5f32e5e4be.jpg', 'ejemplo2.jpg')
48
 
@@ -50,14 +50,14 @@ aws_access_key_id = os.environ['aws_access_key_id']
50
  aws_secret_access_key = os.environ['aws_secret_access_key']
51
  region = os.environ['region']
52
 
53
- #Lista Json y Dataframe
54
  def removeStr(string):
55
  return string.replace(" ", "")
56
 
 
57
  def arrayListax(json_data):
58
  dict = json_data
59
  df2 = json_normalize(dict['detail'])
60
- #df = pd.DataFrame(df2,columns=['Cantidad','Especie'])
61
  return df2
62
 
63
  #Imagen temporal guardada en upload_file
@@ -78,14 +78,14 @@ def upload_file(file_name, bucket=None, object_name=None):
78
  :param object_name: S3 object name. If not specified then file_name is used
79
  :return: Json if file was uploaded, else False
80
  """
81
- # If S3 object_name was not specified, use file_name
82
  if object_name is None:
83
  object_name = os.path.basename(file_name+".jpg")
84
  if bucket is None:
85
  bucket = 'oceanapp'
86
  s3_client = boto3.client('s3',aws_access_key_id=aws_access_key_id,aws_secret_access_key=aws_secret_access_key)
87
  aws_region = boto3.session.Session().region_name
88
- # Upload the file
89
  try:
90
  with open(file_name, "rb") as f:
91
  response = s3_client.upload_fileobj(f, bucket, object_name)
@@ -100,6 +100,7 @@ def upload_file(file_name, bucket=None, object_name=None):
100
  return False
101
  return stado
102
 
 
103
  def qtyEspecies(datax, datay, resImg):
104
  numLobos = 0
105
  numPelicanos = 0
@@ -114,8 +115,7 @@ def qtyEspecies(datax, datay, resImg):
114
  json_data = json.loads(data)
115
  return json_data
116
 
117
-
118
- # Model
119
  model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True, autoshape=True) # local model o google colab
120
 
121
  def yolo(size, iou, conf, im):
@@ -130,18 +130,18 @@ def yolo(size, iou, conf, im):
130
  model.conf = conf
131
  # inference
132
  results2 = model(im)
133
- # updates results.imgs with boxes and labels
134
  results2.render()
135
  results3 = str(results2)
136
- #Contador de especies
137
  results5=results2.pandas().xyxy[0].sort_values('name')
138
- #Transformando la img en bytes
139
  pil_im = Image.fromarray(results2.ims[0])
140
  b = io.BytesIO()
141
  pil_im.save(b, 'jpeg')
142
  im_bytes = b.getvalue()
143
  fileImg = tempFileJSON(im_bytes)
144
- #Enviando la informacion al contador de especies
145
  results6 = qtyEspecies(results5,results3,fileImg)
146
  lista2 = arrayListax(results6)
147
  return Image.fromarray(results2.ims[0]), lista2, results6
 
42
  import io
43
  from PIL import Image
44
  from pandas import json_normalize
45
+ # Imagenes por defecto para HF
46
  torch.hub.download_url_to_file('https://i.pinimg.com/564x/18/0b/00/180b00e454362ff5caabe87d9a763a6f.jpg', 'ejemplo1.jpg')
47
  torch.hub.download_url_to_file('https://i.pinimg.com/564x/3b/2f/d4/3b2fd4b6881b64429f208c5f32e5e4be.jpg', 'ejemplo2.jpg')
48
 
 
50
  aws_secret_access_key = os.environ['aws_secret_access_key']
51
  region = os.environ['region']
52
 
53
+ #Elimina espacios en blanco
54
  def removeStr(string):
55
  return string.replace(" ", "")
56
 
57
+ #Informacion del Dataframe a partir del json
58
  def arrayListax(json_data):
59
  dict = json_data
60
  df2 = json_normalize(dict['detail'])
 
61
  return df2
62
 
63
  #Imagen temporal guardada en upload_file
 
78
  :param object_name: S3 object name. If not specified then file_name is used
79
  :return: Json if file was uploaded, else False
80
  """
81
+ # Si S3 object_name no esta especificado, usa file_name
82
  if object_name is None:
83
  object_name = os.path.basename(file_name+".jpg")
84
  if bucket is None:
85
  bucket = 'oceanapp'
86
  s3_client = boto3.client('s3',aws_access_key_id=aws_access_key_id,aws_secret_access_key=aws_secret_access_key)
87
  aws_region = boto3.session.Session().region_name
88
+ # Subida del archivo
89
  try:
90
  with open(file_name, "rb") as f:
91
  response = s3_client.upload_fileobj(f, bucket, object_name)
 
100
  return False
101
  return stado
102
 
103
+ # Contador de los resultados del modelo
104
  def qtyEspecies(datax, datay, resImg):
105
  numLobos = 0
106
  numPelicanos = 0
 
115
  json_data = json.loads(data)
116
  return json_data
117
 
118
+ # Modelo Yolov5x
 
119
  model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True, autoshape=True) # local model o google colab
120
 
121
  def yolo(size, iou, conf, im):
 
130
  model.conf = conf
131
  # inference
132
  results2 = model(im)
133
+ # actualiza el results.imgs con boxes y labels
134
  results2.render()
135
  results3 = str(results2)
136
+ # contador de especies
137
  results5=results2.pandas().xyxy[0].sort_values('name')
138
+ # transforma la img en bytes
139
  pil_im = Image.fromarray(results2.ims[0])
140
  b = io.BytesIO()
141
  pil_im.save(b, 'jpeg')
142
  im_bytes = b.getvalue()
143
  fileImg = tempFileJSON(im_bytes)
144
+ #Envia la informacion al contador de especies
145
  results6 = qtyEspecies(results5,results3,fileImg)
146
  lista2 = arrayListax(results6)
147
  return Image.fromarray(results2.ims[0]), lista2, results6