Ashrafb commited on
Commit
92105c7
·
verified ·
1 Parent(s): 870a38f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +7 -10
main.py CHANGED
@@ -30,25 +30,22 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
30
  if model is None:
31
  load_model()
32
 
33
- # Save the uploaded image locally with its original filename
34
- with open("uploaded_image.jpg", "wb") as buffer:
35
- shutil.copyfileobj(file.file, buffer)
36
 
37
- # Read the saved image using OpenCV
38
- frame = cv2.imread("uploaded_image.jpg")
39
-
40
- # Convert the image from BGR to RGB
41
- frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
42
 
43
  # Process the uploaded image
44
  aligned_face, instyle, message = model.detect_and_align_image(frame_rgb, top, bottom, left, right)
45
  processed_image, message = model.image_toonify(aligned_face, instyle, model.exstyle, style_degree=0.5, style_type='cartoon1')
46
 
47
  # Convert processed image to bytes
48
- image_bytes = cv2.imencode('.jpg', processed_image)[1].tobytes()
49
 
50
  # Return the processed image as a streaming response
51
- return StreamingResponse(BytesIO(image_bytes), media_type="image/jpeg")
52
 
53
  # Mount static files directory
54
  app.mount("/", StaticFiles(directory="AB", html=True), name="static")
 
30
  if model is None:
31
  load_model()
32
 
33
+ # Read the uploaded image file
34
+ contents = await file.read()
 
35
 
36
+ # Convert the uploaded image to numpy array
37
+ nparr = np.frombuffer(contents, np.uint8)
38
+ frame_rgb = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
 
 
39
 
40
  # Process the uploaded image
41
  aligned_face, instyle, message = model.detect_and_align_image(frame_rgb, top, bottom, left, right)
42
  processed_image, message = model.image_toonify(aligned_face, instyle, model.exstyle, style_degree=0.5, style_type='cartoon1')
43
 
44
  # Convert processed image to bytes
45
+ _, encoded_image = cv2.imencode('.jpg', processed_image)
46
 
47
  # Return the processed image as a streaming response
48
+ return StreamingResponse(io.BytesIO(encoded_image.tobytes()), media_type="image/jpeg")
49
 
50
  # Mount static files directory
51
  app.mount("/", StaticFiles(directory="AB", html=True), name="static")