File size: 1,839 Bytes
9701ff3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import matplotlib.pyplot as plt
from PIL import Image
import csv 
import os

API_URL = "https://api-inference.huggingface.co/models/OttoYu/Tree-Inspection"
headers = {"Authorization": "Bearer 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"}

def TreeAI(image_path):
    def query(filename):
        with open(filename, "rb") as f:
            data = f.read()
        response = requests.post(API_URL, headers=headers, data=data)
        return response.json()

    output = query(image_path)

    if "error" in output:
        print("Error:", output["error"])
    else:
        for result in output:
            label = result["label"]
            confidence = result["score"]
            print("Prediction:", label, ",", confidence, "%")
            
    image = Image.open(image_path)
    plt.imshow(image)
    plt.axis('off')
    plt.show()


def TreeAI_Batch(folder_path, output_csv):
    image_paths = []
    for filename in os.listdir(folder_path):
        if filename.endswith((".jpg", ".jpeg", ".png")):
            image_paths.append(os.path.join(folder_path, filename))
    
    num_images = len(image_paths)
    results = []
    
    for i, image_path in enumerate(image_paths):
        print(f"Processing image {i+1}/{num_images}...")
        
        output = query(image_path)
        
        if "error" in output:
            print("Error:", output["error"])
        else:
            for result in output:
                filename = os.path.basename(image_path)
                label = result["label"]
                confidence = result["score"]
                results.append([filename, label, confidence])
    
    with open(output_csv, "w", newline="") as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["Filename", "Prediction", "Confidence"])
        writer.writerows(results)