add leftover files
parent
d3b8971102
commit
b9def1e911
|
@ -75,3 +75,4 @@ dmypy.json
|
|||
.pytype/
|
||||
cython_debug/
|
||||
/data/
|
||||
.idea/
|
||||
|
|
136
app.py
136
app.py
|
@ -1,42 +1,12 @@
|
|||
import cv2
|
||||
import os
|
||||
import math
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from colors import colorslist
|
||||
import glob
|
||||
import shutil
|
||||
|
||||
# Create the temp dir
|
||||
if not os.path.exists('data'):
|
||||
os.makedirs('data')
|
||||
|
||||
# Define the color spaces
|
||||
Red_lower2 = np.array([,100,100])
|
||||
Red_upper2 = np.array([,255,255])
|
||||
Red_lower1 = np.array([0,100,100])
|
||||
Red_upper1 = np.array([6,255,255])
|
||||
Orange_lower = np.array([7,100,100])
|
||||
Orange_upper = np.array([22,255,255])
|
||||
Yellow_lower = np.array([23,100,100])
|
||||
Yellow_upper = np.array([32,255,255])
|
||||
Green_lower = np.array([33,100,100])
|
||||
Green_upper = np.array([75,255,255])
|
||||
Cyan_lower = np.array([76,100,100])
|
||||
Cyan_upper = np.array([97,255,255])
|
||||
Blue_lower = np.array([93,100,100])
|
||||
Blue_upper = np.array([130,255,255])
|
||||
Purple_lower = np.array([130,100,100])
|
||||
Purple_upper = np.array([,255,255])
|
||||
Pink_lower = np.array([,100,100])
|
||||
Pink_upper = np.array([,255,255])
|
||||
White_lower = np.array([,100,100])
|
||||
White_upper = np.array([,255,255])
|
||||
Black_lower = np.array([,100,100])
|
||||
Black_upper = np.array([,255,255])
|
||||
|
||||
# loop through video files
|
||||
videopath = '/media/vm/HDD/freeloops/3D/'
|
||||
imagepath = 'data/'
|
||||
videos = os.listdir(videopath)
|
||||
for video in videos:
|
||||
def takescreenshots(video):
|
||||
print(video)
|
||||
cap = cv2.VideoCapture(videopath + video)
|
||||
framerate = cap.get(cv2.CAP_PROP_FPS)
|
||||
while cap.isOpened():
|
||||
|
@ -47,13 +17,99 @@ for video in videos:
|
|||
if frameId % math.floor(framerate) == 0:
|
||||
filename = imagepath + "/screenshot_" + str(int(frameId)) + ".jpg"
|
||||
cv2.imwrite(filename, frame)
|
||||
del cap
|
||||
# analyze each screenshot for colors
|
||||
|
||||
|
||||
def getcolors(image):
|
||||
img = cv2.imread(imagepath + image)
|
||||
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
||||
return hsv
|
||||
|
||||
|
||||
def getcolormask(hsv,lower, upper):
|
||||
colormask = cv2.inRange(hsv, lower, upper)
|
||||
return colormask
|
||||
|
||||
|
||||
def getcolorpercentage(mask):
|
||||
percentage = round(((mask > 0).mean()) * 100, 3)
|
||||
return percentage
|
||||
|
||||
|
||||
def calcaverage(color):
|
||||
return sum(color)/len(color)
|
||||
|
||||
|
||||
def gettopcolor(colors):
|
||||
color = max(colors, key=colors.get)
|
||||
result = {color: results.pop(color)}
|
||||
return result
|
||||
|
||||
def movetofolder(folder):
|
||||
if not os.path.exists('/media/vm/HDD/organisedloops/' + folder):
|
||||
os.makedirs('/media/vm/HDD/organisedloops/' + folder)
|
||||
shutil.move(videopath + video, '/media/vm/HDD/organisedloops/' + folder)
|
||||
|
||||
|
||||
# Create the temp dir
|
||||
if not os.path.exists('data'):
|
||||
os.makedirs('data')
|
||||
|
||||
# loop through video files
|
||||
videopath = '/media/vm/HDD/organise/'
|
||||
imagepath = 'data/'
|
||||
videos = os.listdir(videopath)
|
||||
for video in videos:
|
||||
percentagedict = {}
|
||||
for color in colorslist:
|
||||
percentagedict[color['color']] = []
|
||||
|
||||
takescreenshots(video)
|
||||
|
||||
# analyze each screenshot for colors
|
||||
images = os.listdir(imagepath)
|
||||
for image in images:
|
||||
cap = cv2.imread(imagepath + image, cv2.COLOR_BGR2HSV)
|
||||
plt.imshow(cap)
|
||||
hsv = getcolors(image)
|
||||
|
||||
for color in colorslist:
|
||||
mask = getcolormask(hsv, color['lowerlimit'], color['upperlimit'])
|
||||
percentagedict[color['color']].append(getcolorpercentage(mask))
|
||||
|
||||
results = {}
|
||||
for color in colorslist:
|
||||
results[color['color']] = calcaverage(percentagedict[color['color']])
|
||||
results['red'] = results['red1'] + results['red2']
|
||||
del results['red1'], results['red2']
|
||||
|
||||
primarycolor = gettopcolor(results)
|
||||
print(primarycolor)
|
||||
secondarycolor = gettopcolor(results)
|
||||
print(secondarycolor)
|
||||
tertiarycolor = gettopcolor(results)
|
||||
print(tertiarycolor)
|
||||
|
||||
# If color detection fails
|
||||
if next(iter(primarycolor.values())) <= 0.001:
|
||||
folder = 'nofolder'
|
||||
movetofolder(folder)
|
||||
|
||||
|
||||
# if one color dominates
|
||||
elif next(iter(secondarycolor.values())) <= next(iter(primarycolor.values())) * 0.05:
|
||||
folder = next(iter(primarycolor.keys()))
|
||||
movetofolder(folder)
|
||||
|
||||
# move video file according to results
|
||||
else:
|
||||
# if two colors dominate
|
||||
if next(iter(secondarycolor.values())) >= next(iter(tertiarycolor.values())) * 4:
|
||||
folder = next(iter(primarycolor.keys())) + ' + ' + next(iter(secondarycolor.keys()))
|
||||
movetofolder(folder)
|
||||
|
||||
# if three colors dominate
|
||||
else:
|
||||
folder = 'multicolor'
|
||||
movetofolder(folder)
|
||||
|
||||
# clear data folder
|
||||
files = glob.glob('data/*')
|
||||
for f in files:
|
||||
os.remove(f)
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
import numpy as np
|
||||
|
||||
colorslist = [
|
||||
{'color': 'red1', 'lowerlimit': np.array([0, 40, 31]), 'upperlimit': np.array([6, 255, 255])},
|
||||
{'color': 'red2', 'lowerlimit': np.array([175, 40, 31]), 'upperlimit': np.array([180, 255, 255])},
|
||||
{'color': 'orange', 'lowerlimit': np.array([7, 40, 31]), 'upperlimit': np.array([22, 255, 255])},
|
||||
{'color': 'yellow', 'lowerlimit': np.array([26, 40, 31]), 'upperlimit': np.array([32, 255, 255])},
|
||||
{'color': 'green', 'lowerlimit': np.array([39, 40, 31]), 'upperlimit': np.array([70, 255, 255])},
|
||||
{'color': 'cyan', 'lowerlimit': np.array([76, 40, 31]), 'upperlimit': np.array([80, 255, 255])},
|
||||
{'color': 'blue', 'lowerlimit': np.array([93, 40, 31]), 'upperlimit': np.array([128, 255, 255])},
|
||||
{'color': 'purple', 'lowerlimit': np.array([131, 40, 31]), 'upperlimit': np.array([149, 255, 255])},
|
||||
{'color': 'pink', 'lowerlimit': np.array([150, 40, 31]), 'upperlimit': np.array([176, 255, 255])},
|
||||
{'color': 'white', 'lowerlimit': np.array([0, 0, 245]), 'upperlimit': np.array([180, 3, 255])}
|
||||
]
|
Loading…
Reference in New Issue