rewrite this for me so it works correctly: @app.post("/api/phone/camera") async def…
- User
rewrite this for me so it works correctly:
@app.post("/api/phone/camera") async def check_picture(file: UploadFile = File(...)): if not file.content_type.startswith("image/"): return {"error": "File provided is not an image."}
codetry: # Read the contents of the uploaded image image_data = await file.read() image = Image.open(BytesIO(image_data)) # Assuming ocr_reader is defined somewhere and process_images works as intended number = await ocr_reader.process_images(image, "In") return number except Exception as e: return {"error": str(e)}Image processing function
async def process_images(): global SERVER_RESPONSE, TASK_STATUS, check_weight, long_time_no_seen, adress last_frame_change_time = datetime.now() last_active = datetime.now() combined_result = [] failed_frames = 0 support = 0 try: TASK_STATUS = "In progress" logging.info("The process started") print("started") url = f"{adress}/shot.jpg" # frame = cv2.imread("combined_area.png") # url = "http://192.168.127.124:8080/video" # cap = cv2.VideoCapture(url)
codeimg_resp = requests.get(url) img_arr = np.frombuffer(img_resp.content, np.uint8) frame = cv2.imdecode(img_arr, -1) cv2.imwrite("image.jpg", frame) print("hey") with open("image.jpg", "rb") as f: # Send the file in the POST request response = requests.post( "http://localhost:8000/api/phone/camera", files={"file": frame}, ) print("wey") print(response.text) combined_result = response.text if "-" in combined_result: combined_result = combined_result.replace("-", ".") hey_there = await generate_audio(combined_result, True) print(f"{hey_there}") await audio_manager.play_audio_async("audio.mp3") print(f"combined: {combined_result}") await update_csv(combined_result, check_weight, True) # url = "http://10.30.225.127:8080/shot.jpg" # ret, frame = await capture_frame_async(cap) - Assistant