LogicGoInfotechSpaces commited on
Commit
a83b23a
·
verified ·
1 Parent(s): 69f1a20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -13
app.py CHANGED
@@ -26,9 +26,15 @@ from bson import ObjectId
26
  from bson.errors import InvalidId
27
  import httpx
28
  import uvicorn
29
- from PIL import Image
30
  import io
31
  import requests
 
 
 
 
 
 
32
  # DigitalOcean Spaces
33
  import boto3
34
  from botocore.client import Config
@@ -373,9 +379,7 @@ async def get_next_faceswap_sequence():
373
 
374
 
375
  def encode_image_as_jpeg(image_bytes: bytes) -> bytes:
376
- img_bgr = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)
377
- if img_bgr is None:
378
- raise HTTPException(400, "Invalid source image data")
379
 
380
  ok, jpeg_buffer = cv2.imencode(".jpg", img_bgr, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
381
  if not ok:
@@ -383,6 +387,20 @@ def encode_image_as_jpeg(image_bytes: bytes) -> bytes:
383
 
384
  return jpeg_buffer.tobytes()
385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
  # --------------------- Face Swap Pipeline ---------------------
387
  swap_lock = threading.Lock()
388
 
@@ -1006,8 +1024,8 @@ async def face_swap_api(
1006
  response.raise_for_status()
1007
  tgt_bytes = response.content
1008
 
1009
- src_bgr = cv2.imdecode(np.frombuffer(src_bytes, np.uint8), cv2.IMREAD_COLOR)
1010
- tgt_bgr = cv2.imdecode(np.frombuffer(tgt_bytes, np.uint8), cv2.IMREAD_COLOR)
1011
 
1012
  if src_bgr is None or tgt_bgr is None:
1013
  raise HTTPException(400, "Invalid image data")
@@ -1020,7 +1038,7 @@ async def face_swap_api(
1020
  # ------------------------------------------------------------------
1021
  img2_rgb = None
1022
  if img2_bytes:
1023
- img2_bgr = cv2.imdecode(np.frombuffer(img2_bytes, np.uint8), cv2.IMREAD_COLOR)
1024
  if img2_bgr is not None:
1025
  img2_rgb = cv2.cvtColor(img2_bgr, cv2.COLOR_BGR2RGB)
1026
 
@@ -1201,8 +1219,8 @@ async def multi_face_swap_api(
1201
  src_bytes = await source_image.read()
1202
  tgt_bytes = await target_image.read()
1203
 
1204
- src_bgr = cv2.imdecode(np.frombuffer(src_bytes, np.uint8), cv2.IMREAD_COLOR)
1205
- tgt_bgr = cv2.imdecode(np.frombuffer(tgt_bytes, np.uint8), cv2.IMREAD_COLOR)
1206
 
1207
  if src_bgr is None or tgt_bgr is None:
1208
  raise HTTPException(400, "Invalid image data")
@@ -1294,14 +1312,14 @@ async def face_swap_couple_api(
1294
  # -----------------------------
1295
  src_images = []
1296
  img1_bytes = await image1.read()
1297
- src1 = cv2.imdecode(np.frombuffer(img1_bytes, np.uint8), cv2.IMREAD_COLOR)
1298
  if src1 is None:
1299
  raise HTTPException(400, "Invalid image1 data")
1300
  src_images.append(cv2.cvtColor(src1, cv2.COLOR_BGR2RGB))
1301
 
1302
  if image2:
1303
  img2_bytes = await image2.read()
1304
- src2 = cv2.imdecode(np.frombuffer(img2_bytes, np.uint8), cv2.IMREAD_COLOR)
1305
  if src2 is not None:
1306
  src_images.append(cv2.cvtColor(src2, cv2.COLOR_BGR2RGB))
1307
 
@@ -1507,7 +1525,7 @@ async def face_swap_couple_api(
1507
  response.raise_for_status()
1508
  tgt_bytes = response.content
1509
 
1510
- tgt_bgr = cv2.imdecode(np.frombuffer(tgt_bytes, np.uint8), cv2.IMREAD_COLOR)
1511
  if tgt_bgr is None:
1512
  raise HTTPException(400, "Invalid target image data")
1513
 
@@ -3211,4 +3229,4 @@ if __name__ == "__main__":
3211
 
3212
 
3213
  # if __name__ == "__main__":
3214
- # uvicorn.run(fastapi_app, host="0.0.0.0", port=7860)
 
26
  from bson.errors import InvalidId
27
  import httpx
28
  import uvicorn
29
+ from PIL import Image, ImageOps
30
  import io
31
  import requests
32
+ try:
33
+ from pillow_heif import register_heif_opener
34
+ except ImportError:
35
+ register_heif_opener = None
36
+ else:
37
+ register_heif_opener()
38
  # DigitalOcean Spaces
39
  import boto3
40
  from botocore.client import Config
 
379
 
380
 
381
  def encode_image_as_jpeg(image_bytes: bytes) -> bytes:
382
+ img_bgr = decode_image_bytes(image_bytes, "source image")
 
 
383
 
384
  ok, jpeg_buffer = cv2.imencode(".jpg", img_bgr, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
385
  if not ok:
 
387
 
388
  return jpeg_buffer.tobytes()
389
 
390
+
391
+ def decode_image_bytes(image_bytes: bytes, image_label: str = "image"):
392
+ img_bgr = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)
393
+ if img_bgr is not None:
394
+ return img_bgr
395
+
396
+ try:
397
+ pil_img = Image.open(io.BytesIO(image_bytes))
398
+ pil_img = ImageOps.exif_transpose(pil_img).convert("RGB")
399
+ except Exception as exc:
400
+ raise HTTPException(400, f"Invalid {image_label} data") from exc
401
+
402
+ return cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
403
+
404
  # --------------------- Face Swap Pipeline ---------------------
405
  swap_lock = threading.Lock()
406
 
 
1024
  response.raise_for_status()
1025
  tgt_bytes = response.content
1026
 
1027
+ src_bgr = decode_image_bytes(src_bytes, "source image")
1028
+ tgt_bgr = decode_image_bytes(tgt_bytes, "target image")
1029
 
1030
  if src_bgr is None or tgt_bgr is None:
1031
  raise HTTPException(400, "Invalid image data")
 
1038
  # ------------------------------------------------------------------
1039
  img2_rgb = None
1040
  if img2_bytes:
1041
+ img2_bgr = decode_image_bytes(img2_bytes, "second source image")
1042
  if img2_bgr is not None:
1043
  img2_rgb = cv2.cvtColor(img2_bgr, cv2.COLOR_BGR2RGB)
1044
 
 
1219
  src_bytes = await source_image.read()
1220
  tgt_bytes = await target_image.read()
1221
 
1222
+ src_bgr = decode_image_bytes(src_bytes, "source image")
1223
+ tgt_bgr = decode_image_bytes(tgt_bytes, "target image")
1224
 
1225
  if src_bgr is None or tgt_bgr is None:
1226
  raise HTTPException(400, "Invalid image data")
 
1312
  # -----------------------------
1313
  src_images = []
1314
  img1_bytes = await image1.read()
1315
+ src1 = decode_image_bytes(img1_bytes, "image1")
1316
  if src1 is None:
1317
  raise HTTPException(400, "Invalid image1 data")
1318
  src_images.append(cv2.cvtColor(src1, cv2.COLOR_BGR2RGB))
1319
 
1320
  if image2:
1321
  img2_bytes = await image2.read()
1322
+ src2 = decode_image_bytes(img2_bytes, "image2")
1323
  if src2 is not None:
1324
  src_images.append(cv2.cvtColor(src2, cv2.COLOR_BGR2RGB))
1325
 
 
1525
  response.raise_for_status()
1526
  tgt_bytes = response.content
1527
 
1528
+ tgt_bgr = decode_image_bytes(tgt_bytes, "target image")
1529
  if tgt_bgr is None:
1530
  raise HTTPException(400, "Invalid target image data")
1531
 
 
3229
 
3230
 
3231
  # if __name__ == "__main__":
3232
+ # uvicorn.run(fastapi_app, host="0.0.0.0", port=7860)