WildGenie commited on
Commit
25b571b
·
verified ·
0 Parent(s):

initial commit

Browse files
Files changed (4) hide show
  1. .gitattributes +35 -0
  2. README.md +12 -0
  3. app.py +117 -0
  4. requirements.txt +2 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Odev Trajektori Analizi
3
+ emoji: 🔥
4
+ colorFrom: yellow
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 5.10.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import gradio as gr
4
+
5
+ def create_mask(frame):
6
+ """Frame'de kırmızı renk maskesi oluşturur."""
7
+ hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
8
+ mask1 = cv2.inRange(hsv, (0, 120, 70), (10, 255, 255))
9
+ mask2 = cv2.inRange(hsv, (170, 120, 70), (180, 255, 255))
10
+ mask = mask1 | mask2
11
+ return mask
12
+
13
+ def apply_morphology(mask, kernel_size=15):
14
+ """Maskeye morfolojik işlemler uygular."""
15
+ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size))
16
+ morph = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
17
+ return morph
18
+
19
+ def find_largest_component(morph):
20
+ """En büyük bağlı bileşenin merkezini bulur."""
21
+ num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(morph, connectivity=8)
22
+ if num_labels > 1:
23
+ largest_label = 1 + np.argmax(stats[1:, cv2.CC_STAT_AREA])
24
+ cX = int(centroids[largest_label][0])
25
+ cY = int(centroids[largest_label][1])
26
+ return cX, cY
27
+ else:
28
+ return None
29
+
30
+ def draw_trajectory(frame, trajectory):
31
+ """Topun izlediği yolu çizer."""
32
+ if len(trajectory) > 1:
33
+ pts = np.array(trajectory, np.int32)
34
+ pts = pts.reshape((-1, 1, 2))
35
+ cv2.polylines(frame, [pts], False, (0, 255, 0), 2)
36
+
37
+ def analyze_trajectory(trajectory, std_threshold=10):
38
+ """Trajektori analizini yapar."""
39
+ if len(trajectory) < 2:
40
+ return "Yolu belirlemek için yeterli veri yok."
41
+ else:
42
+ x_coords = np.array(trajectory)[:, 0]
43
+ std_x = np.std(x_coords)
44
+ return "ok" if std_x < std_threshold else "not ok"
45
+
46
+ def process_video(video, kernel_size=15, std_threshold=10):
47
+ """Videoyu işler ve topun yörüngesini analiz eder."""
48
+ try:
49
+ cap = cv2.VideoCapture(video)
50
+ fps = cap.get(cv2.CAP_PROP_FPS)
51
+ _, frame = cap.read()
52
+
53
+ if frame is None:
54
+ return "Videodan çerçeve okunamadı!", None, None
55
+
56
+ fourcc = cv2.VideoWriter_fourcc(*'XVID')
57
+ height, width = frame.shape[:2]
58
+ try:
59
+ out_mask = cv2.VideoWriter('mask_output.avi', fourcc, fps, (width, height), False)
60
+ out_final = cv2.VideoWriter('output.avi', fourcc, fps, (width, height))
61
+ except:
62
+ return "Video dosyaları oluşturulamadı!", None, None
63
+
64
+ trajectory = []
65
+ while cap.isOpened():
66
+ ret, frame = cap.read()
67
+ if not ret:
68
+ break
69
+
70
+ mask = create_mask(frame)
71
+ out_mask.write(mask)
72
+
73
+ morph = apply_morphology(mask, kernel_size)
74
+
75
+ center = find_largest_component(morph)
76
+ if center:
77
+ cX, cY = center
78
+ trajectory.append((cX, cY))
79
+ cv2.circle(frame, (cX, cY), 5, (0, 255, 0), -1)
80
+
81
+ draw_trajectory(frame, trajectory)
82
+
83
+ out_final.write(frame)
84
+
85
+ cap.release()
86
+ out_mask.release()
87
+ out_final.release()
88
+
89
+ result_text = analyze_trajectory(trajectory, std_threshold)
90
+
91
+ return result_text, 'mask_output.avi', 'output.avi'
92
+
93
+ except:
94
+ return "Video işlenirken bir hata oluştu!", None, None
95
+
96
+ demo = gr.Interface(
97
+ fn=process_video,
98
+ inputs=[
99
+ gr.Video(label="Girdi Video"),
100
+ gr.Slider(1, 50, value=15, step=1, label="Kernel Boyutu"), # Accordion içindeki bileşenler ayrı ayrı eklendi
101
+ gr.Slider(1, 100, value=10, step=1, label="Standart Sapma Eşiği") # Accordion içindeki bileşenler ayrı ayrı eklendi
102
+ ],
103
+ outputs=[
104
+ gr.Textbox(label="Yol Analiz Sonucu"),
105
+ gr.Video(label="Maske Video"),
106
+ gr.Video(label="Çıktı Video")
107
+ ],
108
+ examples=[
109
+ ["ok.mp4"],
110
+ ["not_ok1.mp4"],
111
+ ["not_ok2.mp4"]
112
+ ]
113
+ )
114
+
115
+ # Launch the Gradio app
116
+ if __name__ == "__main__":
117
+ demo.launch(show_error=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ opencv-python
2
+ numpy