Avmromanov commited on
Commit
454c914
Β·
1 Parent(s): 167935e

car identifier

Browse files
Files changed (1) hide show
  1. app.py +29 -74
app.py CHANGED
@@ -1,86 +1,50 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  from datasets import load_dataset
4
- from PIL import Image
5
- import requests
6
- import io
7
 
8
- # Load your custom dataset
9
  def load_my_dataset():
10
- try:
11
- dataset = load_dataset("Avmromanov/tripoexamples")
12
- return dataset
13
- except Exception as e:
14
- print(f"Error loading dataset: {e}")
15
- return None
 
 
 
 
 
 
 
16
 
17
- # Load car classification model
18
  car_classifier = pipeline("image-classification",
19
  model="dima806/car_models_image_detection")
20
 
21
  def identify_car(image):
22
- if image is None:
23
- return "Please upload an image of a car"
24
 
25
- try:
26
- if image.mode != 'RGB':
27
- image = image.convert('RGB')
28
-
29
- predictions = car_classifier(image)
30
-
31
- result_text = "πŸš— Car Identification Results:\n\n"
32
- top_5 = predictions[:5]
33
-
34
- for i, pred in enumerate(top_5, 1):
35
- label = pred['label'].replace('_', ' ').title()
36
- confidence = pred['score']
37
- result_text += f"{i}. {label}: {confidence:.2%}\n"
38
-
39
- result_text += f"\nπŸ” Most likely: **{top_5[0]['label'].replace('_', ' ').title()}** " \
40
- f"(confidence: {top_5[0]['score']:.2%})"
41
-
42
- return result_text
43
-
44
- except Exception as e:
45
- return f"Error processing image: {str(e)}"
46
-
47
- def get_dataset_examples(dataset, num_examples=3):
48
- """Extract example images from the dataset"""
49
- examples = []
50
 
51
- if dataset is None:
52
- return examples
53
 
54
- try:
55
- # Adjust this based on your dataset structure
56
- train_data = dataset['train']
57
-
58
- for i in range(min(num_examples, len(train_data))):
59
- example = train_data[i]
60
-
61
- # The structure depends on your dataset - adjust accordingly
62
- if 'image' in example:
63
- # If images are stored in the dataset
64
- examples.append(example['image'])
65
- elif 'url' in example:
66
- # If URLs are provided
67
- examples.append(example['url'])
68
- elif 'path' in example:
69
- # If file paths are provided
70
- examples.append(example['path'])
71
-
72
- except Exception as e:
73
- print(f"Error extracting examples: {e}")
74
 
75
- return examples
 
 
 
76
 
77
- # Load your dataset
78
  my_dataset = load_my_dataset()
79
  dataset_examples = get_dataset_examples(my_dataset, num_examples=4)
80
 
81
- # Create the interface
82
  with gr.Blocks() as demo:
83
- gr.Markdown("# πŸš— Car Identifier with My Dataset")
84
  gr.Markdown("Using examples from: **Avmromanov/tripoexamples**")
85
 
86
  with gr.Row():
@@ -90,8 +54,6 @@ with gr.Blocks() as demo:
90
  with gr.Column():
91
  output_text = gr.Textbox(label="Results", lines=10)
92
 
93
-
94
- gr.Markdown(f"### Dataset Examples (showing {len(dataset_examples)} samples)")
95
  gr.Examples(
96
  examples=dataset_examples,
97
  inputs=image_input,
@@ -99,12 +61,5 @@ with gr.Blocks() as demo:
99
  fn=identify_car,
100
  cache_examples=True
101
  )
102
-
103
- gr.Markdown("""
104
- **Dataset Information:**
105
- - Name: tripoexamples
106
- - Author: Avmromanov
107
- - Type: Car images for identification
108
- """)
109
 
110
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from datasets import load_dataset
 
 
 
4
 
5
+
6
  def load_my_dataset():
7
+ dataset = load_dataset("Avmromanov/tripoexamples")
8
+ return dataset
9
+
10
+ def get_dataset_examples(dataset):
11
+ train_data = dataset['train']
12
+ example_ids = [0, 3, 6]
13
+
14
+ examples = []
15
+ for i in example_ids:
16
+ example = train_data[i]
17
+ examples.append(example['image'])
18
+
19
+ return examples
20
 
 
21
  car_classifier = pipeline("image-classification",
22
  model="dima806/car_models_image_detection")
23
 
24
  def identify_car(image):
25
+ if image.mode != 'RGB':
26
+ image = image.convert('RGB')
27
 
28
+ predictions = car_classifier(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+ result_text = "Car Identification Results:\n\n"
31
+ top_5 = predictions[:5]
32
 
33
+ for i, pred in enumerate(top_5, 1):
34
+ label = pred['label'].replace('_', ' ').title()
35
+ confidence = pred['score']
36
+ result_text += f"{i}. {label}: {confidence:.2%}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ result_text += f"\nπŸ” Most likely: **{top_5[0]['label'].replace('_', ' ').title()}** " \
39
+ f"(confidence: {top_5[0]['score']:.2%})"
40
+
41
+ return result_text
42
 
 
43
  my_dataset = load_my_dataset()
44
  dataset_examples = get_dataset_examples(my_dataset, num_examples=4)
45
 
 
46
  with gr.Blocks() as demo:
47
+ gr.Markdown("# Car Identifier with My Dataset")
48
  gr.Markdown("Using examples from: **Avmromanov/tripoexamples**")
49
 
50
  with gr.Row():
 
54
  with gr.Column():
55
  output_text = gr.Textbox(label="Results", lines=10)
56
 
 
 
57
  gr.Examples(
58
  examples=dataset_examples,
59
  inputs=image_input,
 
61
  fn=identify_car,
62
  cache_examples=True
63
  )
 
 
 
 
 
 
 
64
 
65
+ demo.launch()