-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmeans_seg.py
55 lines (45 loc) · 1.46 KB
/
kmeans_seg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#load packages
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
import matplotlib.pyplot as plt
import numpy as np
import argparse
import cv2
import seaborn as sns
import glob
import random
from sklearn.cluster import KMeans
from sklearn.cluster import MiniBatchKMeans
COLORS = [(139, 0, 0),
(0, 100, 0),
(0, 0, 139)]
rgbint = 129
def random_color():
return random.choice(COLORS)
def VideoFrameReaders(VideoDirectory):
cap = cv2.VideoCapture(VideoDirectory)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
fgbg = cv2.createBackgroundSubtractorMOG2()
timestamp = []
count = 0
try:
while cap.isOpened():
ret,frame = cap.read()
time = cap.get(0) #get the frame in seconds
timestamp.append(time)
print timestamp
if frame == None:
break;
# frame = cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
image = frame.reshape((frame.shape[0]*frame.shape[1],3))
K = 4
clf = MiniBatchKMeans(K)
#predict cluster labels and quanitize each color based on the labels
cls_labels = clf.fit_predict(image)
print cls_labels
cls_quant = clf.cluster_centers_astype("uint8")[labels]
except EOFError:
pass
path = glob.glob('/home/sami/Desktop/movies/movies/*.avi');
VideoFrameReaders(path[0])