Mohaddz commited on
Commit
c22d417
·
verified ·
1 Parent(s): 81a3c33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -2,25 +2,35 @@ import gradio as gr
2
  import numpy as np
3
  import tensorflow as tf
4
  from PIL import Image, ImageDraw
5
- import io
6
 
7
- # Load your models here
8
- part_seg_model = tf.keras.models.load_model('path_to_part_seg_model')
9
- damage_seg_model = tf.keras.models.load_model('path_to_damage_seg_model')
10
 
11
  # Define your labels
12
- part_labels = ["front-bumper", "fender", "hood", "door", "trunk"] # Add all your part labels
13
- damage_labels = ["dent", "scratch", "misalignment", "crack"] # Add all your damage labels
 
 
 
 
 
 
14
 
15
  def inference_part_seg(image):
16
- # Implement your part segmentation inference here
17
- # For now, we'll return a random mask
18
- return np.random.randint(0, len(part_labels), size=image.shape[:2])
 
 
19
 
20
  def inference_damage_seg(image):
21
- # Implement your damage segmentation inference here
22
- # For now, we'll return a random mask
23
- return np.random.randint(0, len(damage_labels), size=image.shape[:2])
 
 
24
 
25
  def combine_masks(part_mask, damage_mask):
26
  part_damage_pairs = []
@@ -48,7 +58,7 @@ def create_one_hot_vector(part_damage_pairs):
48
  return vector
49
 
50
  def visualize_results(image, part_mask, damage_mask):
51
- img = Image.fromarray(image)
52
  draw = ImageDraw.Draw(img)
53
 
54
  for i in range(img.width):
 
2
  import numpy as np
3
  import tensorflow as tf
4
  from PIL import Image, ImageDraw
5
+ from transformers import TFSegformerForSemanticSegmentation
6
 
7
+ # Load models from Hugging Face
8
+ part_seg_model = TFSegformerForSemanticSegmentation.from_pretrained("Mohaddz/huggingCars")
9
+ damage_seg_model = TFSegformerForSemanticSegmentation.from_pretrained("Mohaddz/DamageSeg")
10
 
11
  # Define your labels
12
+ part_labels = ["front-bumper", "fender", "hood", "door", "trunk", "cars-8I1q"] # Add all your part labels
13
+ damage_labels = ["dent", "scratch", "misalignment", "crack", "etc"] # Add all your damage labels
14
+
15
+ def preprocess_image(image):
16
+ # Resize and normalize the image
17
+ image = tf.image.resize(image, (512, 512))
18
+ image = tf.keras.applications.imagenet_utils.preprocess_input(image)
19
+ return image
20
 
21
  def inference_part_seg(image):
22
+ preprocessed_image = preprocess_image(image)
23
+ outputs = part_seg_model(preprocessed_image, training=False)
24
+ logits = outputs.logits
25
+ mask = tf.argmax(logits, axis=-1)
26
+ return mask.numpy()
27
 
28
  def inference_damage_seg(image):
29
+ preprocessed_image = preprocess_image(image)
30
+ outputs = damage_seg_model(preprocessed_image, training=False)
31
+ logits = outputs.logits
32
+ mask = tf.argmax(logits, axis=-1)
33
+ return mask.numpy()
34
 
35
  def combine_masks(part_mask, damage_mask):
36
  part_damage_pairs = []
 
58
  return vector
59
 
60
  def visualize_results(image, part_mask, damage_mask):
61
+ img = Image.fromarray((image * 255).astype(np.uint8))
62
  draw = ImageDraw.Draw(img)
63
 
64
  for i in range(img.width):