Mohaddz commited on
Commit
047c754
·
verified ·
1 Parent(s): 558c1bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -1,32 +1,29 @@
1
  import gradio as gr
2
  import numpy as np
3
- import torch
4
  from PIL import Image, ImageDraw
5
- from transformers import SegformerForSemanticSegmentation
6
- from torchvision.transforms import Resize, ToTensor, Normalize
7
 
8
  # Load models from Hugging Face
9
- part_seg_model = SegformerForSemanticSegmentation.from_pretrained("Mohaddz/huggingCars")
10
- damage_seg_model = SegformerForSemanticSegmentation.from_pretrained("Mohaddz/DamageSegMohaddz/DamageSeg")
11
 
12
  # Define your labels
13
  part_labels = ["front-bumper", "fender", "hood", "door", "trunk", "cars-8I1q"] # Add all your part labels
14
  damage_labels = ["dent", "scratch", "misalignment", "crack", "etc"] # Add all your damage labels
15
 
16
  def preprocess_image(image):
17
- # Resize and normalize the image
18
- transform = Resize((512, 512))
19
- image = transform(Image.fromarray(image))
20
- image = ToTensor()(image)
21
- image = Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(image)
22
- return image.unsqueeze(0) # Add batch dimension
23
 
24
  def inference_seg(model, image):
25
- with torch.no_grad():
26
- outputs = model(image)
27
  logits = outputs.logits
28
- mask = torch.argmax(logits, dim=1).squeeze().numpy()
29
- return mask
30
 
31
  def inference_part_seg(image):
32
  preprocessed_image = preprocess_image(image)
@@ -62,7 +59,7 @@ def create_one_hot_vector(part_damage_pairs):
62
  return vector
63
 
64
  def visualize_results(image, part_mask, damage_mask):
65
- img = Image.fromarray(image)
66
  draw = ImageDraw.Draw(img)
67
 
68
  for i in range(img.width):
 
1
  import gradio as gr
2
  import numpy as np
 
3
  from PIL import Image, ImageDraw
4
+ from transformers import TFSegformerForSemanticSegmentation
5
+ import tensorflow as tf
6
 
7
  # Load models from Hugging Face
8
+ part_seg_model = TFSegformerForSemanticSegmentation.from_pretrained("Mohaddz/huggingCars")
9
+ damage_seg_model = TFSegformerForSemanticSegmentation.from_pretrained("Mohaddz/DamageSegMohaddz/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 the image
17
+ image = tf.image.resize(image, (512, 512))
18
+ # Normalize the image
19
+ image = tf.keras.applications.imagenet_utils.preprocess_input(image)
20
+ return tf.expand_dims(image, 0) # Add batch dimension
 
21
 
22
  def inference_seg(model, image):
23
+ outputs = model(image, training=False)
 
24
  logits = outputs.logits
25
+ mask = tf.argmax(logits, axis=-1)
26
+ return mask.numpy().squeeze()
27
 
28
  def inference_part_seg(image):
29
  preprocessed_image = preprocess_image(image)
 
59
  return vector
60
 
61
  def visualize_results(image, part_mask, damage_mask):
62
+ img = Image.fromarray((image * 255).astype('uint8'))
63
  draw = ImageDraw.Draw(img)
64
 
65
  for i in range(img.width):