Skip to content

Instantly share code, notes, and snippets.

View victormurcia's full-sized avatar
😀
Playing with data :]

Victor Murcia victormurcia

😀
Playing with data :]
View GitHub Profile
@victormurcia
victormurcia / load_bc_image.py
Created October 29, 2022 23:13
load bc image
#Get the image from the current working directory
img_name = glob.glob('*.jpg')[0]
#Load the image into my working environment
img = cv2.imread(img_name)
#Show the image
plt.imshow(img)
@victormurcia
victormurcia / color_clustering_GUI.py
Last active October 21, 2022 03:36
gui for viz of clustering results
#Make image bigger
def makeBigger(img):
dim = (300, 200) #(width, height)
# resize image
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
return resized
# empty function called when trackbar moves
def emptyFunction():
@victormurcia
victormurcia / getJerseyColorsFromGame.py
Created October 21, 2022 03:29
process entire soccer game footage for color detection
def getJerseyColorsFromGame(jsonPath,MP4Path,whichVideo,nClusters):
mp4_list = getListOfFiles(MP4Path , ".mp4")
n_mp4 = len(mp4_list)
df_list = []
for vid in range(n_mp4):
jerseyColor_df = getJerseyColorsFromMP4(jsonPath,MP4Path,vid,nClusters)
df_list.append(jerseyColor_df)
allJerseyColors = pd.concat(df_list)
return allJerseyColors
@victormurcia
victormurcia / getJerseyColorsFromMP4.py
Last active October 20, 2022 23:59
get player jersey colors for whole video
def getJerseyColorsFromMP4(jsonPath,MP4Path,whichVideo,nClusters):
#Make the list of mp4 and json files from each camera
print('Retrieving MP4 and JSON files...')
mp4List = getListOfFiles(MP4Path , ".mp4")
jsonList = getListOfFiles(jsonPath , ".json")
#Find the json file to use for the current video
jval = matchJSON2MP4(jsonList, jsonPath, mp4List, MP4Path, whichVideo)
#Get json dictionary of all bounding boxes in video
@victormurcia
victormurcia / crop_image.py
Created October 20, 2022 23:52
crop lower part of player bounding box
def crop_image(image,howMuch):
"""
Args:
img : (array) image of player bounding box
howMuch : (int) percent of image to crop (between 0 and 100)
Returns:
cropped_img : (array) cropped image
"""
val = howMuch/100
cropped_img = image[0:int(image.shape[0]*val),0:int(image.shape[0])]
@victormurcia
victormurcia / KMeansMaskGreen.py
Created October 20, 2022 23:45
kmeans image color with green mask
def KMeansMaskGreen(img, clusters, lowHue, highHue, lowSat, highSat, loBright, hiBright):
"""
Args:
path2img : (str) path to cropped player bounding box
clusters : (int) how many clusters to use for KMEANS
Returns:
rgb_array : (tuple) Dominant colors in image in RGB format
"""
org_img = img.copy()
#print('Org image shape --> ',img.shape)
@victormurcia
victormurcia / KMeansImage.py
Last active October 20, 2022 23:36
KMeans for color detection no mask
def KMeansImage(img, clusters):
"""
Args:
path2img : (str) path to cropped player bounding box
clusters : (int) how many clusters to use for KMEANS
Returns:
rgb_array : (tuple) Dominant colors in image in RGB format
"""
org_img = img.copy()
#Convert image to HSV
@victormurcia
victormurcia / makeRectangleFromJSON.py
Created October 20, 2022 23:07
Extract bounding box coordinates for a specific bounding box in current frame from json
#Extract bounding box coordinates for a specific bounding box in current frame from json
def makeRectangleFromJSON(bb_dict,whichBB):
"""
Args:
bb_dict: (dict) dictionary from json file
whichBB: (int) what bounding box is being processed
Returns:
x1 ,y1 ,x2 ,y2: (tuple) tuple containing pixel coordinates for the upper-left and lower-right corners of the bounding box
"""
x1 ,y1 ,x2 ,y2 = bb_dict[whichBB][0],bb_dict[whichBB][1],bb_dict[whichBB][2],bb_dict[whichBB][3]
@victormurcia
victormurcia / get_bb4frame.py
Created October 20, 2022 23:06
Extract bounding boxes for a given frame from json
#Extract bounding boxes for a given frame from json
def get_bb4frame(bb_dict,frame_index):
"""
Args:
bb_dict: (dict) dictionary from json file
frame: (int) what frame is being processed
Returns:
nDetections: (int) Number of bounding boxes in frame
"""
@victormurcia
victormurcia / findFrameSpacing.py
Created October 20, 2022 23:05
Find first frame that contains detections
#Find first frame that contains detections
def findFrameSpacing(bb_dict):
"""
Args:
bb_dict: (dict) dictionary from json file
Returns:
spacing: (int) Spacing between frames in json
"""
frame0 = bb_dict['frames'][0]['frame_index']