from roboflow import Roboflow import logging import requests from urllib.parse import urlparse import os import uuid rf = Roboflow(api_key="13nZhBkEhxImZULD6bZW") project = rf.workspace("afropalm").project("afropalm-pre-classification") model = project.version(1).model def pre_classification(imageURL): logging.info("Checking if image is red palm oil") response = requests.get(imageURL) if response.status_code == 200: # Parse the URL to get the file name and extension parsed_url = urlparse(imageURL) file_name = parsed_url.path.split("/")[-1] file_extension = file_name.split(".")[-1] id = uuid.uuid4() # Save the downloaded image to a file with a dynamic name and extension filename = f"{id}.{file_extension}" # Save the downloaded image to a file with open(filename, 'wb') as image_file: image_file.write(response.content) # Use the file path of the downloaded image image_path = os.path.dirname(os.path.abspath(filename))+'/'+filename results = model.predict(image_path, confidence=1, overlap=30).json() class_name = None confidence = 0 if results['predictions']: # loop through results and find the highest confidence for result in results["predictions"]: if result["confidence"] > confidence: class_name = result["class"] confidence = result["confidence"] if class_name == "palmoil": return True , image_path else: return False, image_path else: return False, image_path else: logging.error("Error loading the image for pre-classification") raise Exception("Error loading the image")