sergioska commited on
Commit
e0bb95f
·
1 Parent(s): 0911304

fix load image

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -38,21 +38,22 @@ st.title('Upload an image file to detection')
38
 
39
  uploaded_image_zero_file = st.file_uploader("Choose an image file (zero)")
40
  texts = st.text_input('apple, eggs')
41
- image = ''
42
  if uploaded_image_zero_file is not None:
43
- with NamedTemporaryFile() as temp:
44
- image = Image.open(uploaded_image_zero_file)
45
- outputImage = np.array(image)
46
- st.image(outputImage)
47
- inputs = processor(text=texts, images=image, return_tensors="pt")
48
- outputs = model(**inputs)
49
- target_sizes = torch.Tensor([image.size[::-1]])
50
- results = processor.post_process_object_detection(outputs=outputs, threshold=0.1, target_sizes=target_sizes)
51
- i = 0 # Retrieve predictions for the first image for the corresponding text queries
52
- text = texts[i]
53
- boxes, scores, labels = results[i]["boxes"], results[i]["scores"], results[i]["labels"]
54
- st.write(results)
55
- # Print detected objects and rescaled box coordinates
56
- for box, score, label in zip(boxes, scores, labels):
57
- box = [round(i, 2) for i in box.tolist()]
58
- print(f"Detected {text[label]} with confidence {round(score.item(), 3)} at location {box}")
 
 
38
 
39
  uploaded_image_zero_file = st.file_uploader("Choose an image file (zero)")
40
  texts = st.text_input('apple, eggs')
41
+
42
  if uploaded_image_zero_file is not None:
43
+ image = Image.open(uploaded_image_zero_file)
44
+ outputImage = np.array(image)
45
+ st.image(outputImage)
46
+
47
+ if st.button('check tags'):
48
+ inputs = processor(text=texts, images=image, return_tensors="pt")
49
+ outputs = model(**inputs)
50
+ target_sizes = torch.Tensor([image.size[::-1]])
51
+ results = processor.post_process_object_detection(outputs=outputs, threshold=0.1, target_sizes=target_sizes)
52
+ i = 0 # Retrieve predictions for the first image for the corresponding text queries
53
+ text = texts[i]
54
+ boxes, scores, labels = results[i]["boxes"], results[i]["scores"], results[i]["labels"]
55
+ st.write(results)
56
+ # Print detected objects and rescaled box coordinates
57
+ for box, score, label in zip(boxes, scores, labels):
58
+ box = [round(i, 2) for i in box.tolist()]
59
+ print(f"Detected {text[label]} with confidence {round(score.item(), 3)} at location {box}")