File size: 1,916 Bytes
30c5114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed89f9e
30c5114
 
ed89f9e
30c5114
 
ed89f9e
30c5114
 
 
ed89f9e
30c5114
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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")