Documentation & cleaning up

master
Giò Diani 2024-11-23 09:55:25 +01:00
parent bed4bdfe6f
commit f8642cc37e
6 changed files with 16660 additions and 18254 deletions

View File

@ -1,3 +1,6 @@
#!/usr/bin/env python3
import argparse
import os import os
from glob import glob from glob import glob
from pathlib import Path from pathlib import Path
@ -8,6 +11,7 @@ from sklearn.cluster import KMeans
data_folder = str(Path(__file__).parents[3]) + "/data/" data_folder = str(Path(__file__).parents[3]) + "/data/"
# Find the most dominant colors trough k-means clustering
def find_dominant_color(image_path, k=1): def find_dominant_color(image_path, k=1):
# Load the image # Load the image
image = cv2.imread(image_path) image = cv2.imread(image_path)
@ -30,6 +34,7 @@ def find_dominant_color(image_path, k=1):
return tuple(map(tuple, dominant_colors)) return tuple(map(tuple, dominant_colors))
# Convert RGB to HEX
def convert_to_hex(arr): def convert_to_hex(arr):
colors = [] colors = []
@ -39,6 +44,7 @@ def convert_to_hex(arr):
return colors return colors
# Loop trough the frames and get most dominant color
def frame_to_dominant_colors(ep): def frame_to_dominant_colors(ep):
colors = [] colors = []
@ -50,22 +56,31 @@ def frame_to_dominant_colors(ep):
return colors return colors
# Generate a SVG with the most dominant colors
def generate_svg(data, ep): def generate_svg(data, ep):
svg = '' svg = ''
width = len(data) width = len(data)
for i, d in enumerate(data): for i, d in enumerate(data):
for j, cl in enumerate(d): for j, cl in enumerate(d):
svg = svg + f'<rect width="{100 / width}%" x="{(100 / width) *i}%" y="{j * 20}%" height="20%" fill="{cl}" />' svg = svg + f'<rect width="{100 / width}%" x="{(100 / width) *i}%" y="0" height="100%" fill="{cl}" />'
svg = f'<svg version="1.1" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">' + svg + '</svg>' svg = f'<svg version="1.1" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">{svg}</svg>'
queries.save_data_viz(ep, svg) queries.save_data_viz(ep, svg)
return svg return svg
# Initialize the SVG generation
def dominant_color_svg(ep): def dominant_color_svg(ep):
svg = generate_svg(frame_to_dominant_colors(ep), ep) svg = generate_svg(frame_to_dominant_colors(ep), ep)
with open(f"{data_folder}/{ep}/dominant_colors.svg", "w") as f: with open(f"{data_folder}/{ep}/dominant_colors.svg", "w") as f:
f.write(svg) f.write(svg)
return "SVG generated!" return "SVG generated!"
dominant_color_svg("24") # CLI
parser = argparse.ArgumentParser(
prog="Dominant Color Generation", description="Get the most dominant colors of a image and assemble a SVG."
)
parser.add_argument("--episode", "-ep", type=int)
args = parser.parse_args()
dominant_color_svg(args.episode)

View File

@ -1,3 +0,0 @@
#!/bin/bash
area=$(magick $1 -format "%[fx:w*h]" info:)
magick $1 -kmeans 5 -format "%c" histogram:info: | sed 's/://g' | awk -v area=$area '{print 100*$1/area, "%,", $3}' | sed 's/ *//g' | sort -nr -k1,1 -t ","

View File

@ -1,9 +0,0 @@
import subprocess
import time
start = time.time()
path = "/home/gio/Code/VANA/data/25/frame000007.jpg"
img_format = subprocess.getstatusoutput(f'./dominant_colors.sh {path}')
print(img_format)
end = time.time()
print(end - start)

View File

@ -8,9 +8,9 @@
[ [
{ {
"name": "Python venv", "name": "Python venv",
"cmd": ["/home/gio/Code/VANA/VANA-python/normalize_subtitles/.pixi/envs/default/bin/python", "$file"], "cmd": ["/home/gio/Code/VANA/VANA-python/video_deepface2/.pixi/envs/default/bin/python", "$file"],
"selector": "source.python", "selector": "source.python",
"file_regex": "^\\s*File \"(...*?)\", line ([0-9]*)" "file_regex": "^\\s*File \"(...*?)\", line ([0-9]*)"
} }
] ]
} }

File diff suppressed because one or more lines are too long

Binary file not shown.