File size: 1,866 Bytes
f5ede97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import os
import glob

api_url = "https://maftuh-main-batik-classifier.hf.space/predict"
dataset_dir = r"C:\Users\muhammadmaftuh\warisan-digital\ml-service\dataset_batik"

# Test different batik types
test_classes = ["batik-parang", "batik-kawung", "batik-megamendung", "batik-ceplok", "batik-lasem"]

print("\n" + "="*70)
print(" TESTING BATIK CLASSIFIER WITH MULTIPLE PATTERNS")
print("="*70)

correct = 0
total = 0

for batik_class in test_classes:
    class_dir = os.path.join(dataset_dir, batik_class)
    images = glob.glob(os.path.join(class_dir, "*.jpg"))[:1]  # Test 1 image per class
    
    for image_path in images:
        total += 1
        print(f"\n Testing: {batik_class}")
        print(f"   File: {os.path.basename(image_path)}")
        
        try:
            with open(image_path, 'rb') as f:
                files = {'image': (os.path.basename(image_path), f, 'image/jpeg')}
                response = requests.post(api_url, files=files, timeout=30)
            
            if response.status_code == 200:
                result = response.json()
                top_pred = result['predictions'][0]
                confidence = top_pred['confidence'] * 100
                
                is_correct = top_pred['class'] == batik_class
                if is_correct:
                    correct += 1
                    print(f"    Predicted: {top_pred['class']} ({confidence:.1f}%)")
                else:
                    print(f"    Predicted: {top_pred['class']} ({confidence:.1f}%)")
                    print(f"      Expected: {batik_class}")
            else:
                print(f"    Error: {response.status_code}")
        
        except Exception as e:
            print(f"    Exception: {e}")

print("\n" + "="*70)
print(f" RESULTS: {correct}/{total} correct ({correct/total*100:.1f}%)")
print("="*70)