zaferturan commited on
Commit
c5b3184
·
verified ·
1 Parent(s): 43b26e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -32
app.py CHANGED
@@ -1,49 +1,36 @@
1
- import gradio as gr
2
  import torch
3
  import cv2 as cv
4
  from PIL import Image
5
- import numpy as np
6
 
7
  # YOLOv5 modelini yükleyin
8
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
9
 
10
- # CUDA desteğini kontrol et
11
- cuda_enabled = torch.cuda.is_available()
12
- device_label = "Device: GPU" if cuda_enabled else "Device: CPU"
13
-
14
- # Modeli CUDA cihazına taşı
15
- if cuda_enabled:
16
- model.to('cuda')
17
 
18
  def process_video(input_video):
19
  # Giriş ve çıkış video
20
  output_video = "output.mp4"
21
-
22
  # Video yakalama
23
  cap = cv.VideoCapture(input_video)
24
-
25
  # Video özelliklerine bak
26
  fourcc = cv.VideoWriter_fourcc(*'mp4v') # Kodek
27
  fps = int(cap.get(cv.CAP_PROP_FPS)) # Saniyedeki frame sayısı
28
  width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH)) # Genişlik
29
  height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)) # Yükseklik
30
-
31
  out = cv.VideoWriter(output_video, fourcc, fps, (width, height)) # Videoyumuzu belirtilen özelliklerde oluşturur
32
 
33
- # Video çerçevelerini işleme hızını kontrol etmek için bekleme süresi
34
- delay = int(1000 / fps) # Milisaniye cinsinden bekleme süresi
35
-
36
  while cap.isOpened():
37
  ret, frame = cap.read()
38
-
39
- if not ret: # Okuma başarılı ise
40
  break
41
 
42
  # Görüntüyü PIL formatına dönüştür
43
  image = Image.fromarray(cv.cvtColor(frame, cv.COLOR_BGR2RGB))
44
 
45
  # Görüntüyü model ile işleyin
46
- with torch.amp.autocast('cuda'):
47
  results = model(image)
48
 
49
  # Sonuçları alın
@@ -61,32 +48,33 @@ def process_video(input_video):
61
 
62
  # Araba nesnesinin rengini değiştirin
63
  car_roi = frame[int(y1):int(y2), int(x1):int(x2)]
64
-
65
  # Renk kanallarını ayarla
66
  b, g, r = cv.split(car_roi)
67
  rgb_frame = cv.merge((b, r, r))
68
-
69
  # Orijinal çerçeveye geri yaz
70
  frame[int(y1):int(y2), int(x1):int(x2)] = rgb_frame
71
 
72
- # CPU/GPU kullanımını göstermek için etiketi çerçeve üzerine ekleyin
73
- cv.putText(frame, device_label, (10, 30), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv.LINE_AA)
74
 
75
  out.write(frame) # Videoya yaz
76
-
77
  cap.release()
78
  out.release()
 
79
 
 
 
 
80
  return output_video
81
 
82
- # Gradio arayüzünü oluştur
83
- iface = gr.Interface(
84
- fn=process_video,
85
- inputs=gr.Video(label="Input Video"),
86
- outputs=gr.Video(label="Output Video"),
87
- title="Car Detection and Color Change",
88
- description="Upload a video to detect cars and change their color."
89
  )
90
 
91
- # Gradio arayüzünü çalıştır
92
- iface.launch()
 
 
1
  import torch
2
  import cv2 as cv
3
  from PIL import Image
4
+ import gradio as gr
5
 
6
  # YOLOv5 modelini yükleyin
7
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
8
 
9
+ # Modeli CPU üzerinde çalıştır
10
+ model.to('cpu')
 
 
 
 
 
11
 
12
  def process_video(input_video):
13
  # Giriş ve çıkış video
14
  output_video = "output.mp4"
 
15
  # Video yakalama
16
  cap = cv.VideoCapture(input_video)
 
17
  # Video özelliklerine bak
18
  fourcc = cv.VideoWriter_fourcc(*'mp4v') # Kodek
19
  fps = int(cap.get(cv.CAP_PROP_FPS)) # Saniyedeki frame sayısı
20
  width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH)) # Genişlik
21
  height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)) # Yükseklik
 
22
  out = cv.VideoWriter(output_video, fourcc, fps, (width, height)) # Videoyumuzu belirtilen özelliklerde oluşturur
23
 
 
 
 
24
  while cap.isOpened():
25
  ret, frame = cap.read()
26
+ if not ret: # Okuma başarılı değilse
 
27
  break
28
 
29
  # Görüntüyü PIL formatına dönüştür
30
  image = Image.fromarray(cv.cvtColor(frame, cv.COLOR_BGR2RGB))
31
 
32
  # Görüntüyü model ile işleyin
33
+ with torch.no_grad():
34
  results = model(image)
35
 
36
  # Sonuçları alın
 
48
 
49
  # Araba nesnesinin rengini değiştirin
50
  car_roi = frame[int(y1):int(y2), int(x1):int(x2)]
 
51
  # Renk kanallarını ayarla
52
  b, g, r = cv.split(car_roi)
53
  rgb_frame = cv.merge((b, r, r))
 
54
  # Orijinal çerçeveye geri yaz
55
  frame[int(y1):int(y2), int(x1):int(x2)] = rgb_frame
56
 
57
+ # CPU kullanımını göstermek için etiketi çerçeve üzerine ekleyin
58
+ cv.putText(frame, "Device: CPU", (10, 30), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv.LINE_AA)
59
 
60
  out.write(frame) # Videoya yaz
61
+
62
  cap.release()
63
  out.release()
64
+ return output_video
65
 
66
+ # Gradio arayüzü
67
+ def gradio_interface(input_video):
68
+ output_video = process_video(input_video)
69
  return output_video
70
 
71
+ # Gradio arayüzünü başlat
72
+ demo = gr.Interface(
73
+ fn=gradio_interface,
74
+ inputs=gr.Video(label="Input Video"),
75
+ outputs=gr.Video(label="Output Video"),
76
+ title="YOLOv5 Video Processing",
77
+ description="Upload a video to process with YOLOv5 and detect cars."
78
  )
79
 
80
+ demo.launch()