adarsh-007 commited on
Commit
f9dee50
Β·
verified Β·
1 Parent(s): e084a0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -80
app.py CHANGED
@@ -1,80 +1,80 @@
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
-
8
- if os.name == "nt": # Windows
9
- pathlib.PosixPath = pathlib.WindowsPath
10
-
11
- model_path = os.path.join(os.getcwd(), "cards_classification.pkl")
12
- print(f"model path: ", model_path)
13
- learn = load_learner(model_path, cpu=True)
14
-
15
-
16
- def classify_image(image):
17
- if image is None:
18
- return {}
19
- img = PILImage.create(image)
20
- pred_class, pred_idx, probs = learn.predict(img)
21
-
22
- return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}
23
- # return (str(pred_class))
24
-
25
-
26
- CARDS = ['ace of clubs', 'ace of diamonds', 'ace of hearts', 'ace of spades', 'eight of clubs', 'eight of diamonds', 'eight of hearts', 'eight of spades', 'five of clubs', 'five of diamonds', 'five of hearts', 'five of spades', 'four of clubs', 'four of diamonds', 'four of hearts', 'four of spades', 'jack of clubs', 'jack of diamonds', 'jack of hearts', 'jack of spades', 'joker', 'king of clubs', 'king of diamonds', 'king of hearts', 'king of spades', 'nine of clubs', 'nine of diamonds', 'nine of hearts', 'nine of spades', 'queen of clubs', 'queen of diamonds', 'queen of hearts', 'queen of spades', 'seven of clubs', 'seven of diamonds', 'seven of hearts', 'seven of spades', 'six of clubs', 'six of diamonds', 'six of hearts', 'six of spades', 'ten of clubs', 'ten of diamonds', 'ten of hearts', 'ten of spades', 'three of clubs', 'three of diamonds', 'three of hearts', 'three of spades', 'two of clubs', 'two of diamonds', 'two of hearts', 'two of spades']
27
-
28
-
29
- # Create the interface with detailed description
30
- with gr.Blocks() as iface:
31
- gr.Markdown(
32
- """
33
- πŸƒ Playing Card Image Classifier
34
-
35
- This AI model can recognize and classify images of standard playing cards using deep learning.
36
-
37
- ### How it works:
38
- Upload an image of a playing card, and the model will predict which card it is from a full deck of cards.
39
-
40
- ### Supported Cards:
41
- The model can identify **all standard playing cards**, including:
42
- - Aces, numbers (2–10), Jacks, Queens, and Kings
43
- - All four suits: Clubs, Diamonds, Hearts, and Spades
44
- - Joker card
45
-
46
- (In total: 53 classes)
47
-
48
- ### Tips for best results:
49
- - Use clear, high-resolution images
50
- - Make sure the card is fully visible
51
- - Avoid heavy glare, blur, or occlusion
52
- - One card per image works best
53
- """
54
-
55
- )
56
-
57
- with gr.Row():
58
- with gr.Column():
59
- input_image = gr.Image(type="pil", label="Upload Card Image")
60
- submit_btn = gr.Button("πŸ” Identify Card", variant="primary")
61
- with gr.Column():
62
- output_label = gr.Label(num_top_classes=3, label="Predictions")
63
-
64
- gr.Examples(
65
- examples=[
66
- "./test_images/king_of_diamonds.jpg",
67
- "./test_images/jack_of_spades.jpg",
68
- "./test_images/ace_of_diamonds.jpg",
69
- "./test_images/three_of_hearts.jpg",
70
- "./test_images/ace_of_spades.jpg"
71
- ],
72
- inputs=input_image,
73
- label="Try an example"
74
- )
75
-
76
-
77
- submit_btn.click(fn=classify_image, inputs=input_image, outputs=output_label)
78
-
79
- if __name__ == "__main__":
80
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
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
+
8
+ if os.name == "nt": # Windows
9
+ pathlib.PosixPath = pathlib.WindowsPath
10
+
11
+ model_path = os.path.join(os.getcwd(), "cards_classification.pkl")
12
+ print(f"model path: ", model_path)
13
+ learn = load_learner(model_path, cpu=True)
14
+
15
+
16
+ def classify_image(image):
17
+ if image is None:
18
+ return {}
19
+ img = PILImage.create(image)
20
+ pred_class, pred_idx, probs = learn.predict(img)
21
+
22
+ return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}
23
+ # return (str(pred_class))
24
+
25
+
26
+ CARDS = ['ace of clubs', 'ace of diamonds', 'ace of hearts', 'ace of spades', 'eight of clubs', 'eight of diamonds', 'eight of hearts', 'eight of spades', 'five of clubs', 'five of diamonds', 'five of hearts', 'five of spades', 'four of clubs', 'four of diamonds', 'four of hearts', 'four of spades', 'jack of clubs', 'jack of diamonds', 'jack of hearts', 'jack of spades', 'joker', 'king of clubs', 'king of diamonds', 'king of hearts', 'king of spades', 'nine of clubs', 'nine of diamonds', 'nine of hearts', 'nine of spades', 'queen of clubs', 'queen of diamonds', 'queen of hearts', 'queen of spades', 'seven of clubs', 'seven of diamonds', 'seven of hearts', 'seven of spades', 'six of clubs', 'six of diamonds', 'six of hearts', 'six of spades', 'ten of clubs', 'ten of diamonds', 'ten of hearts', 'ten of spades', 'three of clubs', 'three of diamonds', 'three of hearts', 'three of spades', 'two of clubs', 'two of diamonds', 'two of hearts', 'two of spades']
27
+
28
+
29
+ # Create the interface with detailed description
30
+ with gr.Blocks() as iface:
31
+ gr.Markdown(
32
+ """
33
+ πŸƒ Playing Card Image Classifier
34
+
35
+ This AI model can recognize and classify images of standard playing cards using deep learning.
36
+
37
+ ### How it works:
38
+ Upload an image of a playing card, and the model will predict which card it is from a full deck of cards.
39
+
40
+ ### Supported Cards:
41
+ The model can identify **all standard playing cards**, including:
42
+ - Aces, numbers (2–10), Jacks, Queens, and Kings
43
+ - All four suits: Clubs, Diamonds, Hearts, and Spades
44
+ - Joker card
45
+
46
+ (In total: 53 classes)
47
+
48
+ ### Tips for best results:
49
+ - Use clear, high-resolution images
50
+ - Make sure the card is fully visible
51
+ - Avoid heavy glare, blur, or occlusion
52
+ - One card per image works best
53
+ """
54
+
55
+ )
56
+
57
+ with gr.Row():
58
+ with gr.Column():
59
+ input_image = gr.Image(type="pil", label="Upload Card Image")
60
+ submit_btn = gr.Button("πŸ” Identify Card", variant="primary")
61
+ with gr.Column():
62
+ output_label = gr.Label(num_top_classes=3, label="Predictions")
63
+
64
+ gr.Examples(
65
+ examples=[
66
+ "/home/user/app/test_images/king_of_diamonds.jpg",
67
+ "/home/user/app/test_images/jack_of_spades.jpg",
68
+ "/home/user/app/test_images/ace_of_diamonds.jpg",
69
+ "/home/user/app/test_images/three_of_hearts.jpg",
70
+ "/home/user/app/test_images/ace_of_spades.jpg"
71
+ ],
72
+ inputs=input_image,
73
+ label="Try an example"
74
+ )
75
+
76
+
77
+ submit_btn.click(fn=classify_image, inputs=input_image, outputs=output_label)
78
+
79
+ if __name__ == "__main__":
80
+ iface.launch(server_name="0.0.0.0", server_port=7860)