From b9def1e911da8193f3c45d956e5ac345bcada4ff Mon Sep 17 00:00:00 2001 From: git Date: Tue, 23 Jul 2024 12:06:52 +0200 Subject: [PATCH] add leftover files --- .gitignore | 1 + app.py | 136 +++++++++++++++++++++++++++++++++++++---------------- colors.py | 14 ++++++ 3 files changed, 111 insertions(+), 40 deletions(-) create mode 100644 colors.py diff --git a/.gitignore b/.gitignore index d119013..47e18a7 100644 --- a/.gitignore +++ b/.gitignore @@ -75,3 +75,4 @@ dmypy.json .pytype/ cython_debug/ /data/ +.idea/ diff --git a/app.py b/app.py index 40522c5..b6a2517 100644 --- a/app.py +++ b/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) diff --git a/colors.py b/colors.py new file mode 100644 index 0000000..a9fdb7a --- /dev/null +++ b/colors.py @@ -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])} +]