def test_create_video_obj(self): """ test creating Clarifai Video object from different sources """ url = 'https://samples.clarifai.com/3o6gb3kkXfLvdKEZs4.gif' v1 = ClVideo(url=url) self.assertTrue(isinstance(v1, ClVideo)) self.assertEqual(v1.url, url) v1_json = v1.dict() self.assertIn('video', v1_json['data']) self.assertIn('url', v1_json['data']['video']) # test url with leading or trailing spaces url = 'https://samples.clarifai.com/3o6gb3kkXfLvdKEZs4.gif' v1 = ClVideo(url=' ' + url) self.assertEqual(v1.url, url) v1 = ClVideo(url=' ' + url + ' ') self.assertEqual(v1.url, url) with open(video_path, 'rb') as f: video_file_bytes = f.read() # test video file v2 = ClVideo(filename=video_path) toddler_flowers_base64_bytes = base64.b64encode(video_file_bytes) v3 = ClVideo(base64=toddler_flowers_base64_bytes) with open(video_path, 'rb') as f: v4 = ClVideo(file_obj=f)
def test_create_video_obj(self): """ test creating Clarifai Video object from different sources """ v1 = ClVideo(url=sample_inputs.CONAN_GIF_VIDEO_URL) self.assertTrue(isinstance(v1, ClVideo)) self.assertEqual(v1.url, sample_inputs.CONAN_GIF_VIDEO_URL) v1_json = v1.dict() self.assertIn('video', v1_json['data']) self.assertIn('url', v1_json['data']['video']) # test url with leading or trailing spaces v1 = ClVideo(url=' ' + sample_inputs.CONAN_GIF_VIDEO_URL) self.assertEqual(v1.url, sample_inputs.CONAN_GIF_VIDEO_URL) v1 = ClVideo(url=' ' + sample_inputs.CONAN_GIF_VIDEO_URL + ' ') self.assertEqual(v1.url, sample_inputs.CONAN_GIF_VIDEO_URL) with open(sample_inputs.SMALL_VIDEO_FILE_PATH, 'rb') as f: video_file_bytes = f.read() # test video file v2 = ClVideo(filename=sample_inputs.SMALL_VIDEO_FILE_PATH) toddler_flowers_base64_bytes = base64.b64encode(video_file_bytes) v3 = ClVideo(base64=toddler_flowers_base64_bytes) with open(sample_inputs.SMALL_VIDEO_FILE_PATH, 'rb') as f: v4 = ClVideo(file_obj=f)
def video(): form = ReusableForm(request.form) if request.method == 'POST': video_url = request.form['video'] model = cli.models.get('general-v1.3') video = ClVideo(url=video_url) response = model.predict([video]) if form.validate(): flash(response) else: flash(response) return render_template('video.html', form=form)
def video_rekognition(request): if request.method == 'POST': form = VideoForm(request.POST or None, request.FILES or None) if form.is_valid(): print 'Video Form is Valid!' media_file = VideoRekognition(video=request.FILES['upload_video']) media_file.save() file_path = VideoRekognition.objects.latest('video').video video_path = media_path + str(file_path) print 'Video Path:', video_path template_video_path = "/media/" + str(file_path) print 'Template Video Path:', template_video_path video = ClVideo(filename=video_path) clarifai_response = model.predict([video]) # save response locally to display the video contents at the frontend file_name = video_path + '.json' with codecs.open(file_name, 'w', 'utf-8') as ww: json.dump(clarifai_response, ww, ensure_ascii=False, indent=2) # Store the response in a file json_data = json.load(open(file_name)) frame = [] list_data = [] probabilities = {} for elements in json_data['outputs'][0]['data']['frames']: for attributes in elements['data']['concepts']: list_data.append({ 'name': attributes['name'], 'probability': attributes['value'] }) frame.append(list_data) list_data = [] probabilities['data'] = list_data response = json.dumps(frame) context = { "response": response, "video_path": template_video_path } return render(request, "video_response.html", context) else: context = { "form": VideoForm(), } return render(request, "video_form.html", context)
import clarifai from clarifai.rest import ClarifaiApp from clarifai.rest import Video as ClVideo app = ClarifaiApp(api_key='7c4bde0867964706bf73f28d80621980') model = app.models.get('general-v1.3') video = ClVideo( filename='/home/alying/PennApps/Fire-Detector/input-video/0-1.mp4') model.predict([video])
from clarifai.rest import ClarifaiApp from clarifai.rest import Image from clarifai.rest import Video as ClVideo cli = ClarifaiApp(api_key='dbebac7e90f84eabbda37a5f604eaa39') # vid_model = cli.models.get('Galaxy merger Propertiesxc') # vid = Video(file_obj=open('/home/captain/Downloads/videoplayback(10).mp4', 'rb')) # response = vid_model.predict([vid]) model = cli.models.get('general-v1.3') video = ClVideo(url='https://samples.clarifai.com/beer.mp4') response = model.predict([video]) print response
from clarifai.rest import Video as ClVideo from clarifai.rest import ClarifaiApp import json import codecs from pprint import pprint app = ClarifaiApp(api_key='aa53cdc859b84d4d95190962e8613c9a') model = app.models.get('general-v1.3') video = ClVideo(filename='video.mp4') response = model.predict([video]) pprint(response) # Save the response locally in json format file_name = 'output_video' + '.json' with codecs.open(file_name, 'w', 'utf-8') as ww: json.dump(response, ww, ensure_ascii=False, indent=2)