adarsh-007 commited on
Commit
bf53063
ยท
verified ยท
1 Parent(s): 3d27d6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -72
app.py CHANGED
@@ -1,33 +1,12 @@
1
  import gradio as gr
2
  from fastai.vision.all import load_learner, PILImage
3
  import os
4
- from pathlib import Path
5
  import pathlib
6
 
7
  if os.name == "nt": # Windows
8
  pathlib.PosixPath = pathlib.WindowsPath
9
 
10
- model_path = os.path.join(os.getcwd(), "cards_classification.pkl")
11
- print(f"model path: {model_path}")
12
-
13
- # Debug: Print current working directory and list files
14
- print(f"Current working directory: {os.getcwd()}")
15
- print(f"Files in current directory: {os.listdir('.')}")
16
-
17
- # Check if test_images directory exists
18
- if os.path.exists('test_images'):
19
- print(f"test_images directory found!")
20
- print(f"Files in test_images: {os.listdir('test_images')}")
21
- else:
22
- print(f"test_images directory NOT found!")
23
- # Check home directory
24
- home_app = '/home/user/app'
25
- if os.path.exists(home_app):
26
- print(f"Files in {home_app}: {os.listdir(home_app)}")
27
- if os.path.exists(os.path.join(home_app, 'test_images')):
28
- print(f"Files in {home_app}/test_images: {os.listdir(os.path.join(home_app, 'test_images'))}")
29
-
30
- learn = load_learner(model_path, cpu=True)
31
 
32
  def classify_image(image):
33
  if image is None:
@@ -36,25 +15,11 @@ def classify_image(image):
36
  pred_class, pred_idx, probs = learn.predict(img)
37
  return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}
38
 
39
- CARDS = ['ace of clubs', 'ace of diamonds', 'ace of hearts', 'ace of spades',
40
- 'eight of clubs', 'eight of diamonds', 'eight of hearts', 'eight of spades',
41
- 'five of clubs', 'five of diamonds', 'five of hearts', 'five of spades',
42
- 'four of clubs', 'four of diamonds', 'four of hearts', 'four of spades',
43
- 'jack of clubs', 'jack of diamonds', 'jack of hearts', 'jack of spades',
44
- 'joker', 'king of clubs', 'king of diamonds', 'king of hearts', 'king of spades',
45
- 'nine of clubs', 'nine of diamonds', 'nine of hearts', 'nine of spades',
46
- 'queen of clubs', 'queen of diamonds', 'queen of hearts', 'queen of spades',
47
- 'seven of clubs', 'seven of diamonds', 'seven of hearts', 'seven of spades',
48
- 'six of clubs', 'six of diamonds', 'six of hearts', 'six of spades',
49
- 'ten of clubs', 'ten of diamonds', 'ten of hearts', 'ten of spades',
50
- 'three of clubs', 'three of diamonds', 'three of hearts', 'three of spades',
51
- 'two of clubs', 'two of diamonds', 'two of hearts', 'two of spades']
52
-
53
- # Create the interface with detailed description
54
  with gr.Blocks() as iface:
55
  gr.Markdown(
56
  """
57
- ๐Ÿƒ Playing Card Image Classifier
58
 
59
  This AI model can recognize and classify images of standard playing cards using deep learning.
60
 
@@ -68,7 +33,6 @@ with gr.Blocks() as iface:
68
  - Joker card (In total: 53 classes)
69
 
70
  ### Tips for best results:
71
- - Use clear, high-resolution images
72
  - Make sure the card is fully visible
73
  - Avoid heavy glare, blur, or occlusion
74
  - One card per image works best
@@ -82,39 +46,17 @@ with gr.Blocks() as iface:
82
  with gr.Column():
83
  output_label = gr.Label(num_top_classes=3, label="Predictions")
84
 
85
- # Use absolute paths or check which path works
86
- example_paths = []
87
- test_dir = "test_images"
88
-
89
- # Try to find the correct path
90
- if os.path.exists(test_dir):
91
- base_path = test_dir
92
- elif os.path.exists("/home/user/app/test_images"):
93
- base_path = "/home/user/app/test_images"
94
- else:
95
- base_path = None
96
- print("WARNING: Could not find test_images directory!")
97
-
98
- if base_path:
99
- example_files = ["king_of_diamonds.jpg", "jack_of_spades.jpg",
100
- "ace_of_diamonds.jpg", "three_of_hearts.jpg", "ace_of_spades.jpg"]
101
-
102
- for filename in example_files:
103
- full_path = os.path.join(base_path, filename)
104
- if os.path.exists(full_path):
105
- example_paths.append([full_path])
106
- print(f"Found example: {full_path}")
107
- else:
108
- print(f"Missing example: {full_path}")
109
-
110
- if example_paths:
111
- gr.Examples(
112
- examples=example_paths,
113
- inputs=input_image,
114
- label="Try an example"
115
- )
116
- else:
117
- gr.Markdown("โš ๏ธ Example images not found. Please upload your own image.")
118
 
119
  submit_btn.click(fn=classify_image, inputs=input_image, outputs=output_label)
120
 
 
1
  import gradio as gr
2
  from fastai.vision.all import load_learner, PILImage
3
  import os
 
4
  import pathlib
5
 
6
  if os.name == "nt": # Windows
7
  pathlib.PosixPath = pathlib.WindowsPath
8
 
9
+ learn = load_learner("cards_classification.pkl", cpu=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def classify_image(image):
12
  if image is None:
 
15
  pred_class, pred_idx, probs = learn.predict(img)
16
  return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}
17
 
18
+ # Create the interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  with gr.Blocks() as iface:
20
  gr.Markdown(
21
  """
22
+ # ๐Ÿƒ Playing Card Image Classifier
23
 
24
  This AI model can recognize and classify images of standard playing cards using deep learning.
25
 
 
33
  - Joker card (In total: 53 classes)
34
 
35
  ### Tips for best results:
 
36
  - Make sure the card is fully visible
37
  - Avoid heavy glare, blur, or occlusion
38
  - One card per image works best
 
46
  with gr.Column():
47
  output_label = gr.Label(num_top_classes=3, label="Predictions")
48
 
49
+ gr.Examples(
50
+ examples=[
51
+ ["test_images/king_of_diamonds.jpg"],
52
+ ["test_images/jack_of_spades.jpg"],
53
+ ["test_images/ace_of_diamonds.jpg"],
54
+ ["test_images/three_of_hearts.jpg"],
55
+ ["test_images/ace_of_spades.jpg"]
56
+ ],
57
+ inputs=input_image,
58
+ label="Try an example"
59
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  submit_btn.click(fn=classify_image, inputs=input_image, outputs=output_label)
62