JoshuaCarrera commited on
Commit
3921ea2
·
1 Parent(s): c3c8af8

Añadir logs en txt

Browse files
Files changed (1) hide show
  1. src/app/app.py +36 -8
src/app/app.py CHANGED
@@ -157,21 +157,49 @@ def detection_vef(image: UploadFile):
157
  vzla_tz = timezone(timedelta(hours=-4))
158
  now = datetime.now(vzla_tz)
159
 
160
- # Formato: DD-MM-YYYY_HHMMSS_<label>_<confianza>.jpg
161
- # Usamos :.4f para limitar la confianza a 4 decimales (ej. 0.9876)
162
- filename = f"{now.strftime('%d-%m-%Y_%H%M%S')}_{primary_label}_{primary_conf:.4f}.jpg"
163
- r2_key = f"logs/{filename}"
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  s3_client = get_s3_client()
 
 
166
  s3_client.put_object(
167
  Bucket=R2_BUCKET_NAME,
168
- Key=r2_key,
169
- Body=imageBytes, # Usamos los bytes originales para no perder calidad
170
  ContentType="image/jpeg"
171
  )
172
- print(f"[LOG] Imagen respaldada en R2: {r2_key}")
 
 
 
 
 
 
 
 
 
 
173
  except Exception as e:
174
- print(f"❌ [ERROR] Fallo al subir la imagen a logs en R2: {e}")
175
  # -----------------------------------------------------
176
 
177
  boxes = [
 
157
  vzla_tz = timezone(timedelta(hours=-4))
158
  now = datetime.now(vzla_tz)
159
 
160
+ # Formato base del nombre
161
+ base_filename = f"{now.strftime('%d-%m-%Y_%H%M%S')}_{primary_label}_{primary_conf:.4f}"
 
 
162
 
163
+ # Keys correctas con sus extensiones para R2
164
+ r2_image_key = f"logs/{base_filename}.jpg"
165
+ r2_text_key = f"logs/{base_filename}.txt"
166
+
167
+ # 1. Preparar las coordenadas del TXT en formato YOLO (norm_x_center, norm_y_center, norm_width, norm_height)
168
+ log_lines = []
169
+ for box in results[0].boxes:
170
+ class_id = int(box.cls.item())
171
+ # xywhn devuelve valores normalizados entre 0 y 1
172
+ x_center, y_center, width, height = box.xywhn[0].tolist()
173
+
174
+ line = f"{class_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}\n"
175
+ log_lines.append(line)
176
+
177
+ # Convertimos el string acumulado en bytes listos para S3/R2
178
+ text_bytes = "".join(log_lines).encode("utf-8")
179
+
180
+ # 2. Subir ambos archivos usando el mismo cliente S3
181
  s3_client = get_s3_client()
182
+
183
+ # Subir Imagen
184
  s3_client.put_object(
185
  Bucket=R2_BUCKET_NAME,
186
+ Key=r2_image_key,
187
+ Body=imageBytes,
188
  ContentType="image/jpeg"
189
  )
190
+ print(f"[LOG] Imagen respaldada en R2: {r2_image_key}")
191
+
192
+ # Subir TXT de anotaciones
193
+ s3_client.put_object(
194
+ Bucket=R2_BUCKET_NAME,
195
+ Key=r2_text_key,
196
+ Body=text_bytes,
197
+ ContentType="text/plain" # ContentType correcto para archivos de texto limpio
198
+ )
199
+ print(f"[LOG] TXT de anotaciones respaldado en R2: {r2_text_key}")
200
+
201
  except Exception as e:
202
+ print(f"❌ [ERROR] Fallo al subir logs a R2: {e}")
203
  # -----------------------------------------------------
204
 
205
  boxes = [