m8chaa commited on
Commit
27328ef
·
verified ·
1 Parent(s): dc4d307

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -286
app.py CHANGED
@@ -161,16 +161,18 @@ async def mobile_process_receipts(background_tasks: BackgroundTasks, request: Re
161
  data = await request.json()
162
  base64_str = data['base64Image']
163
  prefix, base64_str = base64_str.split(',', 1) # Split on the first comma
164
- image_contents = base64.b64decode(base64_str)
165
  content_type = data['mimeType']
166
  sheeturl = data['sheetID']
167
 
168
  checkPhoto = {
169
  'sheetID': sheeturl.split('/')[-2],
170
  'mode': "check",
171
- 'photo': image_contents,
 
 
172
  }
173
- async with httpx.AsyncClient(follow_redirects=True) as client:
174
  response = await client.post(appsScriptsURL, json=checkPhoto)
175
  if response.status_code != 200:
176
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
@@ -178,7 +180,7 @@ async def mobile_process_receipts(background_tasks: BackgroundTasks, request: Re
178
  if response.json()['status'] == 'invalid':
179
  return {"message": "Invalid photo."}
180
 
181
-
182
  uuid = str(uuid4())
183
  timestamp = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
184
  message = "영수기 AI가 데이터 분석 중입니다. 잠시만 기다려주세요."
@@ -306,285 +308,3 @@ async def mobile_process_receipts(background_tasks: BackgroundTasks, sheeturl: s
306
  raise HTTPException(status_code=500, detail="Error from external API.")
307
  background_tasks.add_task(background_worker, sheeturl, image_contents, image.filename, image.content_type, appsScriptsURL)
308
  return {"message": "Image processing completed."}
309
-
310
- # async def failure(sheetid):
311
- # async with httpx.AsyncClient(follow_redirects=True) as client:
312
- # response = await client.post(
313
- # 'https://script.google.com/macros/s/AKfycbw9_31pMwQVhJPezJ9qlMRCAamQKRREnun-tfT_TnS5TOwOVcfdgU1Y9xTpAv6bZuiKOA/exec',
314
- # json={"sheetID": sheetid, "mode": "failed"}
315
- # )
316
- # if response.status_code != 200:
317
- # logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
318
- # raise HTTPException(status_code=500, detail="Error from external API.")
319
-
320
- # async def download_image(url: str):
321
- # async with httpx.AsyncClient(follow_redirects=True) as client:
322
- # # Your existing code to stream the response
323
- # # Use .stream() instead of .get() for streaming
324
- # async with client.stream("GET", url) as resp:
325
- # if resp.status_code == 200:
326
- # content = b''
327
- # async for chunk in resp.aiter_bytes():
328
- # content += chunk
329
- # return content
330
- # else:
331
- # return None
332
- #
333
- # async def save_image(image_data, image):
334
- # s3_client = boto3.client('s3')
335
-
336
- # bucket_name = 're-sheet'
337
- # object_name = image_data['fileName']
338
-
339
- # response = s3_client.put_object(
340
- # Bucket=bucket_name,
341
- # Key=object_name,
342
- # Body=image,
343
- # ContentType=image_data['mimeType']
344
- # )
345
-
346
- # status = response.get("ResponseMetadata", {}).get("HTTPStatusCode")
347
- # if status == 200:
348
- # message = "File uploaded successfully to S3"
349
- # image_url = f"https://{bucket_name}.s3.amazonaws.com/{object_name}"
350
- # body = {"queued":
351
- # [{
352
- # "s3Url": image_url,
353
- # "sheetID": image_data['sheetID'],
354
- # "type": "process-receipts"
355
- # },],
356
- # "mode": "queue",
357
- # }
358
-
359
- # async with httpx.AsyncClient(follow_redirects=True) as client:
360
- # response = await client.post(
361
- # 'https://script.google.com/macros/s/AKfycbw9_31pMwQVhJPezJ9qlMRCAamQKRREnun-tfT_TnS5TOwOVcfdgU1Y9xTpAv6bZuiKOA/exec',
362
- # json=body
363
- # )
364
- # logging.info(f"Response from Google Apps Script API: {response.status_code} - {response.text}")
365
- # if response.status_code != 200:
366
- # logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
367
- # failure(image_data['sheetID'])
368
- # raise HTTPException(status_code=500, detail="Error from external API.")
369
-
370
- # @app.post("/save-to-s3")
371
- # async def save_to_s3(request: Request, background_tasks: BackgroundTasks):
372
- # image_data = await request.json()
373
-
374
- # image = await download_image(image_data['image_url'])
375
- # if image is None:
376
- # raise HTTPException(status_code=500, detail="Cannot download image from google drive.")
377
-
378
- # background_tasks.add_task(save_image, image_data, image)
379
- # return {"message": "Image download started. Processing in the background."}
380
-
381
-
382
- # @app.post("/upload-and-save")
383
- # async def upload_and_save(background_tasks: BackgroundTasks, image: UploadFile = File(...), sheet_url: str = Form(...)):
384
- # image_contents = await image.read()
385
- # sheet_id = sheet_url.split('/')[-2]
386
- # image_data = {
387
- # 'sheetID': sheet_id,
388
- # 'fileName': image.filename,
389
- # 'mimeType': image.content_type
390
- # }
391
-
392
- # background_tasks.add_task(save_image, image_data, image_contents)
393
- # return {"message": "Image upload started. Processing in the background."}
394
-
395
-
396
- # def order_points(pts):
397
- # rect = np.zeros((4, 2), dtype="float32")
398
- # s = pts.sum(axis=1)
399
- # rect[0] = pts[np.argmin(s)]
400
- # rect[2] = pts[np.argmax(s)]
401
- # diff = np.diff(pts, axis=1)
402
- # rect[1] = pts[np.argmin(diff)]
403
- # rect[3] = pts[np.argmax(diff)]
404
- # return rect
405
-
406
- # def angle_between_vectors_degrees(u, v):
407
- # """Returns the angle in degrees between vectors 'u' and 'v'."""
408
- # cos_theta = np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))
409
- # angle = np.arccos(np.clip(cos_theta, -1.0, 1.0))
410
- # return np.degrees(angle)
411
-
412
- # def calculate_angles(contour):
413
- # angles = []
414
- # contour = contour.reshape((4, 2))
415
- # for i in range(4):
416
- # prev_vertex = contour[i - 1]
417
- # current_vertex = contour[i]
418
- # next_vertex = contour[(i + 1) % 4]
419
-
420
- # vector1 = prev_vertex - current_vertex
421
- # vector2 = next_vertex - current_vertex
422
-
423
- # angle = angle_between_vectors_degrees(vector1, vector2)
424
- # angles.append(angle)
425
- # return angles
426
-
427
- # def detectReceipt(masked_image, original_image):
428
- # original_aspect_ratio = original_image.shape[1] / original_image.shape[0]
429
- # masked_aspect_ratio = masked_image.shape[1] / masked_image.shape[0]
430
-
431
- # if abs(original_aspect_ratio - masked_aspect_ratio) > 0.1: # Threshold can be adjusted
432
- # # Rotate the masked image 90 degrees clockwise
433
- # masked_image = cv2.rotate(masked_image, cv2.ROTATE_90_CLOCKWISE)
434
-
435
- # original_image_shape = original_image.shape[:2]
436
-
437
- # resized_masked_image = cv2.resize(masked_image, (original_image_shape[1], original_image_shape[0]))
438
-
439
-
440
-
441
- # blurred_masked_image = cv2.GaussianBlur(resized_masked_image, (5, 5), 0)
442
- # _, thresh_masked_image = cv2.threshold(blurred_masked_image, 175, 255, cv2.THRESH_BINARY)
443
- # edges = cv2.Canny(thresh_masked_image, 75, 150)
444
- # contours, hierarchy = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
445
-
446
- # base64_images = []
447
- # padding = 20
448
- # for contour in contours:
449
- # perimeter = cv2.arcLength(contour, True)
450
- # approximation = cv2.approxPolyDP(contour, 0.02 * perimeter, True)
451
- # x, y, w, h = cv2.boundingRect(approximation)
452
-
453
- # # Add padding to the bounding box coordinates while ensuring they are within the image boundaries
454
- # x_pad = max(x - padding, 0)
455
- # y_pad = max(y - padding, 0)
456
- # w_pad = min(w + 2 * padding, original_image.shape[1] - x_pad)
457
- # h_pad = min(h + 2 * padding, original_image.shape[0] - y_pad)
458
-
459
- # # logging.info(f"Bounding box: x={x_pad}, y={y_pad}, w={w_pad}, h={h_pad}")
460
-
461
- # # Check if the area of the bounding rectangle is greater than 50px x 50px
462
- # if w_pad > 150 or h_pad > 150:
463
- # cropped_image = original_image[y_pad:y_pad + h_pad, x_pad:x_pad + w_pad]
464
- # _, img_encoded = cv2.imencode('.png', cropped_image)
465
- # base64_str = base64.b64encode(img_encoded).decode('utf-8')
466
- # base64_images.append(base64_str)
467
-
468
- # return base64_images
469
-
470
-
471
- # # process images as background tasks
472
- # async def process_image(original_contents, s3_client, object_key, bucket_name, image, original_image):
473
-
474
- # logging.info("Preparing request to Dezgo API")
475
-
476
- # headers = {
477
- # 'X-Dezgo-Key': 'DEZGO-AA84DEB300562DA089E56CEA6081AE7DB601EF2D3EE4C693910E1C25E59A4FF8023D4932'
478
- # }
479
- # files = {
480
- # 'image': ('filename', original_contents),
481
- # 'mode': 'mask',
482
- # }
483
-
484
- # logging.info("Sending request to Dezgo API")
485
- # async with httpx.AsyncClient(timeout=timeout) as client:
486
- # response = await client.post('https://api.dezgo.com/remove-background', headers=headers, files=files)
487
- # if response.status_code != 200:
488
- # logging.error(f"Unexpected response from Dezgo API: {response.status_code} - {response.text}")
489
- # failure(image['sheetID'])
490
- # raise HTTPException(status_code=500, detail="Error from external API.")
491
- # masked_image_data = response.content
492
-
493
- # if not masked_image_data:
494
- # logging.error("Received empty masked image data from Dezgo API.")
495
- # failure(image['sheetID'])
496
- # raise HTTPException(status_code=500, detail="Received empty data from external API.")
497
-
498
- # nparr_masked = np.frombuffer(masked_image_data, np.uint8)
499
- # if nparr_masked.size == 0:
500
- # logging.error("Failed to convert API response to numpy array.")
501
- # failure(image['sheetID'])
502
- # raise HTTPException(status_code=500, detail="Error processing API response data.")
503
-
504
- # masked_image = cv2.imdecode(nparr_masked, cv2.IMREAD_COLOR)
505
- # if masked_image is None:
506
- # logging.error("Failed to decode image data.")
507
- # failure(image['sheetID'])
508
- # raise HTTPException(status_code=500, detail="Error decoding image data.")
509
-
510
- # # Call the detectReceipt function to process the image
511
- # base64_images = detectReceipt(masked_image, original_image)
512
-
513
-
514
-
515
- # queued = []
516
- # for i, base64_str in enumerate(base64_images, start=1):
517
- # image_bytes = base64.b64decode(base64_str)
518
- # object_name = f"{object_key}_processed_{i}.png"
519
-
520
- # # Upload the processed image to S3
521
- # response = s3_client.put_object(
522
- # Bucket=bucket_name,
523
- # Key=object_name,
524
- # Body=image_bytes,
525
- # ContentType='image/png'
526
- # )
527
- # print('saving to s3')
528
-
529
- # status = response.get("ResponseMetadata", {}).get("HTTPStatusCode")
530
- # if status == 200:
531
- # url_encoded_object_name = urllib.parse.quote(object_name)
532
- # processed_image_url = f"https://{bucket_name}.s3.amazonaws.com/{url_encoded_object_name}"
533
- # body = {
534
- # "s3Url": processed_image_url,
535
- # "sheetID": image['sheetID'],
536
- # "mode": "queue",
537
- # "type": "read-receipt"
538
- # }
539
- # queued.append(body)
540
- # else:
541
- # logging.error(f"Failed to upload processed image to S3: {object_name}")
542
- # body = {"queued": queued, "mode": "queue", }
543
- # async with httpx.AsyncClient(follow_redirects=True) as client:
544
- # response = await client.post(
545
- # 'https://script.google.com/macros/s/AKfycbw9_31pMwQVhJPezJ9qlMRCAamQKRREnun-tfT_TnS5TOwOVcfdgU1Y9xTpAv6bZuiKOA/exec',
546
- # json=body
547
- # )
548
- # if response.status_code != 200:
549
- # failure(image['sheetID'])
550
- # logging.error(
551
- # f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
552
- # raise HTTPException(status_code=500, detail="Error from external API.")
553
-
554
- # @app.post("/process-receipts")
555
- # async def process_receipts(request: Request, background_tasks: BackgroundTasks):
556
- # image = await request.json()
557
- # logging.info(f"Received file: {image['s3Url']}")
558
-
559
- # s3_client = boto3.client('s3')
560
- # bucket_name = image["s3Url"].split('/')[2].split('.')[0] # Extract bucket name from the URL
561
- # object_key = '/'.join(image["s3Url"].split('/')[3:]) # Extract object key from the URL
562
-
563
- # try:
564
- # # Download the image from S3
565
- # s3_response = s3_client.get_object(Bucket=bucket_name, Key=object_key)
566
- # original_contents = s3_response['Body'].read() # gets the image as bytes
567
-
568
- # logging.info("Decoding the original image for processing")
569
- # nparr_original = np.frombuffer(original_contents, np.uint8)
570
- # original_image = cv2.imdecode(nparr_original, cv2.IMREAD_COLOR)
571
-
572
- # if original_image is None:
573
- # logging.error("Failed to decode the uploaded image.")
574
- # raise HTTPException(status_code=500, detail="Failed to decode the uploaded image.")
575
-
576
- # background_tasks.add_task(process_image, original_contents, s3_client, object_key, bucket_name, image, original_image)
577
-
578
- # return {"message": "image queued for processing"}
579
-
580
- # except Exception as e:
581
- # error_msg = f"Error during processing: {e}\n{traceback.format_exc()}"
582
- # failure(image['sheetID'])
583
- # logging.error(error_msg)
584
- # raise HTTPException(status_code=500, detail=error_msg)
585
-
586
- # finally:
587
- # logging.info("File processing completed")
588
-
589
- # else:
590
- # raise HTTPException(status_code=400, detail="Email address not provided.")
 
161
  data = await request.json()
162
  base64_str = data['base64Image']
163
  prefix, base64_str = base64_str.split(',', 1) # Split on the first comma
164
+
165
  content_type = data['mimeType']
166
  sheeturl = data['sheetID']
167
 
168
  checkPhoto = {
169
  'sheetID': sheeturl.split('/')[-2],
170
  'mode': "check",
171
+ 'photo': base64_str,
172
+ 'mimeType': content_type,
173
+ 'fileName': 'test.png'
174
  }
175
+ async with httpx.AsyncClient(timeout=300, follow_redirects=True) as client:
176
  response = await client.post(appsScriptsURL, json=checkPhoto)
177
  if response.status_code != 200:
178
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
 
180
  if response.json()['status'] == 'invalid':
181
  return {"message": "Invalid photo."}
182
 
183
+ image_contents = base64.b64decode(base64_str)
184
  uuid = str(uuid4())
185
  timestamp = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
186
  message = "영수기 AI가 데이터 분석 중입니다. 잠시만 기다려주세요."
 
308
  raise HTTPException(status_code=500, detail="Error from external API.")
309
  background_tasks.add_task(background_worker, sheeturl, image_contents, image.filename, image.content_type, appsScriptsURL)
310
  return {"message": "Image processing completed."}