leedoming commited on
Commit
70e228b
·
verified ·
1 Parent(s): 7b52fd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -19
app.py CHANGED
@@ -38,25 +38,33 @@ if api_key:
38
  CLIENT = setup_roboflow_client(api_key)
39
 
40
  def segment_image(image_path):
41
- results = CLIENT.infer(image_path, model_id="closet/1")
42
- # json.loads() 제거, results를 직접 사용
43
-
44
- image = cv2.imread(image_path)
45
- image = cv2.resize(image, (800, 600))
46
- mask = np.zeros(image.shape, dtype=np.uint8)
47
-
48
- for prediction in results['predictions']:
49
- points = prediction['points']
50
- pts = np.array([[p['x'], p['y']] for p in points], np.int32)
51
- scale_x = image.shape[1] / results['image']['width']
52
- scale_y = image.shape[0] / results['image']['height']
53
- pts = pts * [scale_x, scale_y]
54
- pts = pts.astype(np.int32)
55
- pts = pts.reshape((-1, 1, 2))
56
- cv2.fillPoly(mask, [pts], color=(255, 255, 255)) # White mask
57
-
58
- segmented_image = cv2.bitwise_and(image, mask)
59
- return Image.fromarray(cv2.cvtColor(segmented_image, cv2.COLOR_BGR2RGB))
 
 
 
 
 
 
 
 
60
 
61
  def get_image_embedding(image):
62
  image_tensor = preprocess_val(image).unsqueeze(0).to(device)
 
38
  CLIENT = setup_roboflow_client(api_key)
39
 
40
  def segment_image(image_path):
41
+ try:
42
+ results = CLIENT.infer(image_path, model_id="closet/1")
43
+
44
+ image = cv2.imread(image_path)
45
+ image = cv2.resize(image, (800, 600))
46
+ mask = np.zeros(image.shape, dtype=np.uint8)
47
+
48
+ if 'predictions' in results:
49
+ for prediction in results['predictions']:
50
+ points = prediction['points']
51
+ pts = np.array([[p['x'], p['y']] for p in points], np.int32)
52
+ scale_x = image.shape[1] / results['image']['width']
53
+ scale_y = image.shape[0] / results['image']['height']
54
+ pts = pts * [scale_x, scale_y]
55
+ pts = pts.astype(np.int32)
56
+ pts = pts.reshape((-1, 1, 2))
57
+ cv2.fillPoly(mask, [pts], color=(255, 255, 255)) # White mask
58
+
59
+ segmented_image = cv2.bitwise_and(image, mask)
60
+ else:
61
+ st.warning("No predictions found in the image. Returning original image.")
62
+ segmented_image = image
63
+
64
+ return Image.fromarray(cv2.cvtColor(segmented_image, cv2.COLOR_BGR2RGB))
65
+ except Exception as e:
66
+ st.error(f"Error in segmentation: {str(e)}")
67
+ return Image.open(image_path) # Return original image if segmentation fails
68
 
69
  def get_image_embedding(image):
70
  image_tensor = preprocess_val(image).unsqueeze(0).to(device)