This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Find first frame that contains detections | |
| def findFirstFrame(bb_dict): | |
| """ | |
| Args: | |
| bb_dict: (dict) dictionary from json file | |
| Returns: | |
| firstFrame: (int) First frame to process in video | |
| """ | |
| firstFrame = bb_dict['frames'][0]['frame_index'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Determine number of bounding boxes in frame | |
| def count_bboxes(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 | |
| """ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Get dictionary from json file | |
| def read_json_dict(path2json): | |
| """ | |
| Args: | |
| path2json: (str) path to .MP4 json file containing player bounding boxes | |
| Returns: | |
| bb_dict: (dict) Dictionary containing bounding boxes in each frame | |
| """ | |
| # Opening JSON file | |
| f = open(path2json) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def matchJSON2MP4(jsonList, jsonPath, MP4list, MP4Path, whichMP4): | |
| json_strip = [s.replace(directory + jsonPath + '\\', '') for s in jsonList] | |
| json_strip = [s.replace(".json", '') for s in json_strip] | |
| mp4_strip = [s.replace(directory + MP4Path + '\\', '') for s in MP4list] | |
| mp4Name = mp4_strip[whichMP4] | |
| index = json_strip.index(mp4Name) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Determine number of frames in video | |
| def count_frames(video_file): | |
| """ | |
| Args: | |
| video_file: (str) path to .MP4 video file | |
| Returns: | |
| nFrames: (int) Number of frames in mp4 | |
| """ | |
| cap = cv2.VideoCapture(video_file) | |
| length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_frame(video_file, frame_index): | |
| """ | |
| Args: | |
| video_file: (str) path to .MP4 video file | |
| frame_index: (int) query frame index | |
| Returns: | |
| frame: (ndarray, size (y, x, 3)) video frame | |
| Uses OpenCV BGR channels | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def getListOfFiles(rPath , fType): | |
| """ | |
| Args: | |
| rPath: (str) path to file | |
| fType: (str) type of file to look for (i.e., .mp4, .json, etc.) | |
| Returns: | |
| lFiles: (list) List of files in rPath of type fType | |
| """ | |
| #1. Establish the current working directory | |
| directory = os.getcwd() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Detecting humans with HOG | |
| path2xml = r'C:\Users\vmurc\Documents\GitHub\opencv\data\haarcascades\haarcascade_fullbody.xml' | |
| fbCascade = cv2.CascadeClassifier(path2xml) | |
| # Initializing the HOG person detector | |
| image = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2GRAY) | |
| hog = cv2.HOGDescriptor() | |
| hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) | |
| # Resizing the Image |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def KMeansTest(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() | |
| #print('Org image shape --> ',img.shape) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Determine optimal k value for clustering using elbow method | |
| distortions = [] #Initialize array with distortions from each clustering run | |
| K = range(1,11) #Explore k values between 1 and 10 | |
| #Run the clustering routine | |
| for k in K: | |
| #Convert image into a 1D array | |
| flat_img = np.reshape(rgb_img,(-1,3)) | |
| kmeanModel = KMeans(n_clusters=k) | |
| kmeanModel.fit(flat_img) | |
| distortions.append(kmeanModel.inertia_) |