adi-123 commited on
Commit
fe70c63
·
verified ·
1 Parent(s): 64b6344

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -27
app.py CHANGED
@@ -22,36 +22,12 @@ def detect_rhino_image(image):
22
  cv2.putText(image, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)), cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
23
  return image
24
 
25
- def detect_rhino_video(video_file):
26
- cap = cv2.VideoCapture(video_file.name)
27
- ret, frame = cap.read()
28
- H, W, _ = frame.shape
29
- out = cv2.VideoWriter(video_file.name + '_output.mp4', cv2.VideoWriter_fourcc(*'MP4V'), int(cap.get(cv2.CAP_PROP_FPS)), (W, H))
30
- while ret:
31
- results = model(frame)[0]
32
- for box in results.boxes.data.tolist():
33
- x1, y1, x2, y2, score, class_id = box
34
- if score > 0.5:
35
- cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
36
- cv2.putText(frame, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)), cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
37
- out.write(frame)
38
- ret, frame = cap.read()
39
- cap.release()
40
- out.release()
41
- cv2.destroyAllWindows()
42
- return video_file.name + '_output.mp4'
43
-
44
  st.title('Rhinoceros Detection App')
45
 
46
- st.write("Upload an image or video of rhinoceroses for detection.")
47
 
48
- file = st.file_uploader("Choose a file...", type=["jpg", "jpeg", "png", "mp4"])
49
  if file is not None:
50
  if file.type.split('/')[0] == 'image':
51
  image = Image.open(file)
52
- st.image(detect_rhino_image(image), caption='Processed Image', use_column_width=True)
53
- elif file.type.split('/')[0] == 'video':
54
- tfile = tempfile.NamedTemporaryFile(delete=False)
55
- tfile.write(file.read())
56
- processed_video = detect_rhino_video(tfile)
57
- st.video(processed_video)
 
22
  cv2.putText(image, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)), cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
23
  return image
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  st.title('Rhinoceros Detection App')
26
 
27
+ st.write("Upload an image for rhino detection.")
28
 
29
+ file = st.file_uploader("Choose a file...", type=["jpg", "jpeg", "png"])
30
  if file is not None:
31
  if file.type.split('/')[0] == 'image':
32
  image = Image.open(file)
33
+ st.image(detect_rhino_image(image), caption='Processed Image', use_column_width=True)