def _init_clarifai_limits(self): app_id, app_secret = json.loads(self.redis.lrange("clarifai", 0, 0)[0]) api = ClarifaiApi(app_id, app_secret) info = api.get_info() self.max_image_bytes = info.get(u'max_image_bytes', self.config['DEFAULT_MAX_IMG_BYTES'])
class Video_Tag_Extract(): def __init__(self, video_name): self.working_root = '..' self.videos_root = '../training_videos' self.images_root = '../training_images' self.json_root = '../jsons' self.video_name = video_name self.modulus = 20 self.api = ClarifaiApi() def extract_images_from_video(self, video_start, video_end, job_id): """ input self.video_name output json file """ # set up video_range = range(video_start, video_end) cap = cv2.VideoCapture(os.path.join(self.videos_root, self.video_name)) num_frames = cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) fps = cap.get(cv2.cv.CV_CAP_PROP_FPS) self.modulus = int(1.5*fps) print self.modulus width = cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH) height = cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT) print width print height # assert False cur_frame = 0 while cur_frame < num_frames: print cur_frame has_frame, frame = cap.read() # frame is (height, width, channels) if cur_frame in video_range: cv2.imshow('frame', frame) # write frame if (cur_frame - video_start) % self.modulus == 0: img_filename = os.path.join(self.images_root, self.video_name[:self.video_name.find('.mp4')] + '_' + str(cur_frame) + '_job_id=' + str(job_id) + '.jpg') cv2.imwrite(img_filename, frame) print 'Written' k = cv2.waitKey(30) & 0xff if k == 27: break cur_frame += 1 cv2.destroyAllWindows() cap.release() def analyze_images(self, job_id): # analyze images in batch image_list = [x for x in os.listdir(self.images_root) if 'job_id=' + str(job_id) + '.jpg' in x][:self.api.get_info()['max_batch_size']] result = self.api.tag_images([open(os.path.join(self.images_root, x)) for x in image_list])['results'] info = {} for idx in range(len(result)): info[str(idx)] = result[idx]['result']['tag'] with open('data.json', 'w') as fp: json.dump(info, fp) def remove_images(self, video_start, video_end, job_id): image_list = [x for x in os.listdir(self.images_root) if 'job_id=' + str(job_id) + '.jpg' in x]#[:self.api.get_info()['max_batch_size']] for img_name in image_list: os.system('rm ' + os.path.join(self.images_root, img_name))
def test_get_info(self): api = ClarifaiApi() response = api.get_info() self.assertTrue(response.get('api_version')) self.assertTrue(len(response) > 0)
def test_get_info(self): api = ClarifaiApi() response = api.get_info() self.assertTrue(response.get("api_version")) self.assertTrue(len(response) > 0)