def _save_image(self, dpoint: str, img_filepath: str) -> None: """Save image to filesystem.""" if not os.path.exists(img_filepath): # Downloading image from url as numpy array image_np = url_to_image(dpoint) cv2.imwrite( # pylint: disable=no-member img_filepath, np.flip(image_np, axis=2))
def show_data(self, start: int = 0, end: int = 20) -> None: """Show the video data.""" ims = [] fig, ax = plt.subplots() num_frames = len(self.items_list) for i in tqdm(range(start, min(end, num_frames))): frame = url_to_image(url=self.items_list[i]) height, width, _ = frame.shape im = plt.imshow(frame, animated=True) ims_ = self.labels.show( ax=ax, width=width, height=height, frameindex=i, num_frames=len(self.items_list), ) ims_ = [im] + ims_ # the frame data im needs to be first ims.append(ims_) animation.ArtistAnimation( fig, ims, interval=50, blit=True, repeat_delay=1000, ) plt.show()
def test_url_to_image_invalid() -> None: """Test the utility function with invalid urls""" url = "https://not-a-real-url.jpg" url2 = "http://127.0.0.1:8080/not-real.jpg" # first image try: url_to_image(url) assert False except: assert True # second image try: url_to_image(url2) assert False except: assert True
def test_url_to_image() -> None: """Test the conversion of a url to image data.""" url = "https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg" local = "tests/Example.jpg" # get local image # pylint: disable=no-member img_local = cv2.imread(local) img_local = np.flip(img_local, axis=2) # get remote image img_remote = url_to_image(url) comparison = img_local == img_remote assert comparison.all()
def get_data(presigned_url: str) -> np.ndarray: """Get the frame/image data.""" return url_to_image(presigned_url)
def get_datapoint( self, org_id: str, label_set_name: str, dp_id: str, task_type: str, taxonomy: dict, ) -> Union[Image, Video]: """Get all relevant information related to a datapoint.""" query_string = """ query ($orgId: UUID!, $dpId: UUID!, $name:String!) { labelData(orgId: $orgId, dpId: $dpId, customGroupName: $name) { blob dataPoint { items:items(presigned:true) items_not_signed:items(presigned:false) name }, createdBy, taskType, dataType, labels { category, attributes { ... on LabelAttributeInt { name valint: value } ... on LabelAttributeBool { name valbool: value } ... on LabelAttributeFloat { name valfloat: value } ... on LabelAttributeString { name valstr: value } } labelid, frameclassify, frameindex, trackid, keyframe, end, bbox2d { xnorm, ynorm, wnorm, hnorm, }, pixel { imagesize, regions, holes } } } } """ # EXECUTE THE QUERY query_variables = { "orgId": org_id, "name": label_set_name, "dpId": dp_id } query = dict(query=query_string, variables=query_variables) result = self._execute_query(query) # IMAGE DATA if result["labelData"]["dataType"] == "IMAGE": # Parse result # print("RESULT", result) with open("temp.json", "w+") as file: json.dump(result, file, indent=2) signed_image_url = result["labelData"]["dataPoint"]["items"][0] unsigned_image_url = result["labelData"]["dataPoint"][ "items_not_signed"][0] labels = result["labelData"]["labels"] created_by = result["labelData"]["createdBy"] image_data = url_to_image(signed_image_url) # Get image array dpoint = Image( created_by=created_by, org_id=org_id, label_set_name=label_set_name, taxonomy=taxonomy, remote_labels=labels, dp_id=dp_id, image_url=signed_image_url, image_url_not_signed=unsigned_image_url, image_data=image_data, task_type=task_type, ) return dpoint # VIDEO DATA else: # Parse the result items = result["labelData"]["dataPoint"]["items"] items_not_signed = result["labelData"]["dataPoint"][ "items_not_signed"] labels = result["labelData"]["labels"] with open("result.json", "w+") as file: json.dump(result, file, indent=2) name = result["labelData"]["dataPoint"]["name"] dpoint_vid = Video( org_id=org_id, label_set_name=label_set_name, taxonomy=taxonomy, task_type=task_type, remote_labels=labels, dp_id=dp_id, video_name=name, items_list=items, items_list_not_signed=items_not_signed, ) return dpoint_vid
def cache_bbox(self) -> None: """Cache video bounding boxes.""" # Create YOLO style meta data files num_classes = len(list(self.labelset.taxonomy.keys())) train_file = "train.txt" names_file = "names.txt" data_file = "obj.data" backup_dir = "backup/" # obj.data with open(self.cache_dir + "/" + data_file, "w+") as file: file.write("classes = %s" % num_classes) file.write("\n") file.write("train = %s" % ("data" + "/" + train_file)) file.write("\n") file.write("names = %s" % ("data" + "/" + names_file)) file.write("\n") file.write("backup = %s" % backup_dir) # names.txt with open(self.cache_dir + "/" + names_file, "w+") as file: class_names = list(self.labelset.taxonomy.keys()) for name in class_names: file.write(name + "\n") # obj_train_data/ os.mkdir(os.path.join(self.cache_dir, "obj_train_data")) taxonomy_mapper = { name: idx for idx, name in enumerate(list(self.labelset.taxonomy.keys())) } image_filepaths = [] for i in range(len(self.labelset.dp_ids)): print( colored("[INFO]:", "blue"), "Exporting %s/%s video(s)" % (i + 1, len(self.labelset.dp_ids)), ) # Create tqdm loader dp = self.labelset.__getitem__(i) pbar = tqdm(total=len(dp.items_list)) # Interpolate the video labels, and get per frame labels framelabels = dp.labels.interpolate_labels( num_frames=len(dp.items_list)) # Iterate through video frames for idx, item in enumerate(dp.items_list): pbar.update(1) dp_entry = {} dp_entry["url"] = dp.items_list_not_signed[idx] dp_entry["labels"] = [] # write image data to file image_filepath = os.path.join( self.cache_dir, "obj_train_data", str(dp.items_list_not_signed[idx].replace("/", "_").replace( ":", "_")), ) image_filepaths.append( os.path.join( "obj_train_data", str(dp.items_list_not_signed[idx].replace( "/", "_").replace(":", "_")), )) frame_data = np.flip(url_to_image(item), axis=2) cv2.imwrite(image_filepath, frame_data) # create the label file name filext_idx = image_filepath.rfind(".") if not filext_idx: filext_idx = -1 label_filepath = image_filepath[0:filext_idx] + ".txt" # write labels to the txt file frame_labels = framelabels[idx] # write labels to the txt file with open(label_filepath, "w+") as file: for label in frame_labels: class_idx = taxonomy_mapper[label.category] file.write("%d %.6f %.6f %.6f %.6f \n" % ( class_idx, label.xnorm, label.ynorm, label.wnorm, label.hnorm, )) pbar.close() # create train.txt file with open(os.path.join(self.cache_dir, train_file), "w+") as file: for filename in image_filepaths: file.write(filename + "\n")
def cache_classify(self) -> None: """Cache video classification.""" output = [] for i in range(len(self.labelset.dp_ids)): print( colored("[INFO]:", "blue"), "Exporting %s/%s video(s)" % (i + 1, len(self.labelset.dp_ids)), ) dp = self.labelset.__getitem__(i) video_name = dp.video_name labels = dp.labels # Loop through the key frame labels, and export by interpolating # the non key frames. start_idx = labels.labels[0].frameindex end_idx = len(dp.items_list) curr_idx = start_idx # the current real frame index frame_label = labels.labels[curr_idx].category[0][-1] key_index = 0 # the current key frame index pbar = tqdm(total=end_idx) while curr_idx != end_idx: # check if frame is key frame, if yes get the new labels, else keep old if (key_index < len(labels.labels) and labels.labels[key_index].frameindex == curr_idx): frame_label = labels.labels[key_index].category[0][-1] key_index += 1 # check export format type if self.format == "redbrick-json": output.append({ "url": dp.items_list_not_signed[curr_idx], "class": frame_label, "name": video_name, }) else: # export the frame to the relvant folder if not os.path.isdir( os.path.join(self.cache_dir, video_name, frame_label)): os.makedirs( os.path.join(self.cache_dir, video_name, frame_label)) frame_data = np.flip(url_to_image(dp.items_list[curr_idx]), axis=2) frame_filename = ( dp.items_list_not_signed[curr_idx].replace( "/", "_").replace(":", "_")) cv2.imwrite( os.path.join(self.cache_dir, video_name, frame_label, frame_filename), frame_data, ) # update the index curr_idx += 1 pbar.update(1) pbar.close() if self.format == "redbrick-json": with open(os.path.join(self.cache_dir, "output.json"), "w+") as file: json.dump(output, file, indent=2)