EdBoy2202 commited on
Commit
bd96a0e
·
verified ·
1 Parent(s): 7ff0ab2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -18
app.py CHANGED
@@ -3,11 +3,8 @@ import pandas as pd
3
  import openai
4
  import joblib
5
  from PIL import Image
6
- import requests
7
- from io import BytesIO
8
  import matplotlib.pyplot as plt
9
  import numpy as np
10
- from sklearn.preprocessing import LabelEncoder
11
  from huggingface_hub import hf_hub_download
12
  from transformers import AutoFeatureExtractor, AutoModelForImageClassification
13
  import torch
@@ -24,9 +21,6 @@ def load_datasets():
24
  st.error(f"Error loading dataset: {str(e)}")
25
  raise e
26
 
27
- def load_image(image_file):
28
- return Image.open(image_file)
29
-
30
  def classify_image(image):
31
  try:
32
  # Load the model and feature extractor
@@ -114,6 +108,10 @@ model = load_model_and_encodings()
114
  # Initialize OpenAI API key
115
  openai.api_key = st.secrets["GPT_TOKEN"]
116
 
 
 
 
 
117
  # File uploader for image
118
  uploaded_file = st.file_uploader("Choose a car image", type=["jpg", "jpeg", "png"])
119
 
@@ -121,40 +119,35 @@ uploaded_file = st.file_uploader("Choose a car image", type=["jpg", "jpeg", "png
121
  camera_image = st.camera_input("Or take a picture of the car")
122
 
123
  # Process the image (either uploaded or from camera)
124
- image = None
125
- st.write(f"uploaded_file: {uploaded_file}")
126
- st.write(f"camera_image: {camera_image}")
127
-
128
  if uploaded_file is not None:
129
  st.write("Attempting to open uploaded file...")
130
  try:
131
- image = Image.open(uploaded_file)
132
  st.write("Image uploaded successfully.")
133
  except Exception as e:
134
  st.error(f"Error opening uploaded file: {str(e)}")
135
  elif camera_image is not None:
136
  st.write("Attempting to open camera image...")
137
  try:
138
- image = Image.open(camera_image)
139
  st.write("Image captured successfully.")
140
  except Exception as e:
141
  st.error(f"Error opening camera image: {str(e)}")
142
 
143
- st.write(f"image: {image}")
144
-
145
- if image is not None:
146
- st.image(image, caption='Processed Image', use_container_width=True)
147
 
148
  # Classify the car image
149
  with st.spinner('Analyzing image...'):
150
- car_classifications = classify_image(image)
151
 
152
  if car_classifications:
153
  st.write("Image classification successful.")
154
  st.subheader("Car Classification Results:")
155
  for classification in car_classifications:
156
  st.write(f"Model: {classification['label']}")
157
- st.write(f"Confidence: {classification['score']*100:.2f}%")
158
 
159
  # Use the top prediction for further processing
160
  top_prediction = car_classifications[0]['label']
 
3
  import openai
4
  import joblib
5
  from PIL import Image
 
 
6
  import matplotlib.pyplot as plt
7
  import numpy as np
 
8
  from huggingface_hub import hf_hub_download
9
  from transformers import AutoFeatureExtractor, AutoModelForImageClassification
10
  import torch
 
21
  st.error(f"Error loading dataset: {str(e)}")
22
  raise e
23
 
 
 
 
24
  def classify_image(image):
25
  try:
26
  # Load the model and feature extractor
 
108
  # Initialize OpenAI API key
109
  openai.api_key = st.secrets["GPT_TOKEN"]
110
 
111
+ # Get the session state
112
+ if 'image' not in st.session_state:
113
+ st.session_state.image = None
114
+
115
  # File uploader for image
116
  uploaded_file = st.file_uploader("Choose a car image", type=["jpg", "jpeg", "png"])
117
 
 
119
  camera_image = st.camera_input("Or take a picture of the car")
120
 
121
  # Process the image (either uploaded or from camera)
 
 
 
 
122
  if uploaded_file is not None:
123
  st.write("Attempting to open uploaded file...")
124
  try:
125
+ st.session_state.image = Image.open(uploaded_file)
126
  st.write("Image uploaded successfully.")
127
  except Exception as e:
128
  st.error(f"Error opening uploaded file: {str(e)}")
129
  elif camera_image is not None:
130
  st.write("Attempting to open camera image...")
131
  try:
132
+ st.session_state.image = Image.open(camera_image)
133
  st.write("Image captured successfully.")
134
  except Exception as e:
135
  st.error(f"Error opening camera image: {str(e)}")
136
 
137
+ # Display the processed image
138
+ if st.session_state.image is not None:
139
+ st.image(st.session_state.image, caption='Processed Image', use_container_width=True)
 
140
 
141
  # Classify the car image
142
  with st.spinner('Analyzing image...'):
143
+ car_classifications = classify_image(st.session_state.image)
144
 
145
  if car_classifications:
146
  st.write("Image classification successful.")
147
  st.subheader("Car Classification Results:")
148
  for classification in car_classifications:
149
  st.write(f"Model: {classification['label']}")
150
+ st.write(f"Confidence: {classification['score'] * 100:.2f}%")
151
 
152
  # Use the top prediction for further processing
153
  top_prediction = car_classifications[0]['label']