Video upload

Here is an example Python code snippet on how to upload videos using Neurons API

import os
import requests

def upload_video(apikey: str = '', filepath: str = ''):
	endpoint = 'https://api.neuronsinc.com/predict/v1/videos/upload'

	# EVALUATE FILE
	_, ext = os.path.splitext(filepath)
	video_format = ext[1:]
	TOTAL_SIZE = os.path.getsize(filepath)

	# INITIALIZE THE UPLOAD
	headers = {
		'X-API-Key': apikey,
		'content-type': 'application/json'
	}
	payload = {
		'content_type': f'video/{video_format}',
		'total_bytes': str(TOTAL_SIZE)
	}
	response = requests.post(
		url=endpoint,
		headers=headers,
		json=payload
	)

	# YELL IF WE'RE NOT GETTING A USABLE "RESUMABLE URL"
	if response.status_code != 200:
		sys.exit(f'START\nSTATUS CODE: {response.status_code}\nTEXT: {response.text}')

	# READ THE RESPONSE AND GET RESUMABLE URL AND MEDIA ID
	d = response.json()
	resumable_url = d['resumable_url']
	media_id = d['media_id']
	print(f'media_id: {media_id}')

	# DO THE ACTUAL UPLOAD
	# SPECIFY CHUNK SIZE
	TARGET_CHUNK_SIZE = 1024 * 1024 * 8

	# GET THE TOTAL SIZE OF THE FILE FOR THE UPLOAD HEADERS
	with open(filepath, 'rb') as f:

		# COUNT THE UPLOADED BYTES SO FAR
		BYTES_UPLOADED = 0
		CHUNK_NO = 1

		# ITERATE OVER THE CHUNKS OF THE FILE
		while chunk_bytes := f.read(TARGET_CHUNK_SIZE):

			# GET THE ACTUAL CHUNK SIZE
			CHUNK_SIZE = len(chunk_bytes)

			# CONSTRUCT HEADERS FOR THIS PARTICULAR CHUNK
			chunk_headers = {
				'Content-Length': str(CHUNK_SIZE),
				'Content-Range': f'bytes {BYTES_UPLOADED}-{BYTES_UPLOADED + CHUNK_SIZE - 1}/{TOTAL_SIZE}'
			}

			# UPLOAD THE CHUNK
			response = requests.put(
				url=resumable_url,
				data=chunk_bytes,
				headers=chunk_headers
			)

			# INCREMENT THE UPLOADED BYTES
			BYTES_UPLOADED += CHUNK_SIZE

			# PRINT STATUS
			print(f'CHUNK {CHUNK_NO}\tSIZE {CHUNK_SIZE}\tSTATUS: {response.status_code}\tRESPONSE: {response.text}')

			# INCREMENT THE CHUNK NO
			CHUNK_NO += 1

	# FINALIZE THE UPLOAD
	media_id = d['media_id']
	response = requests.put(
		url=f'{endpoint}/{media_id}',
		json={"status": "done"},
		headers=headers
	)

	# PRINT FINALIZATION STATUS
	print(f'FINALIZE_STATUS_CODE:\t{response.status_code}')
	print(response.json())


if __name__ == '__main__':
	upload_video('API_KEY', 'FILE_PATH')

Once the video is uploaded, you can call Get Video endpoint to return results of the predicted media.
Kindly remember, the results will only be shown once the prediction is finished.

Sample response once the media is not predicted, is as shown below:

{
  "media_type": "video",
  "media_id": "UUID",
  "status": "started",
  "results": {}
}

Sample response once the media is predicted, is as shown below:

{
  "media_type": "video",
  "media_id": "UUID",
  "status": "done",
  "results": {
    "fog": "https://signed-url.com",
    "heat": "https://signed-url.com",
    "metrics": "https://signed-url.com",
    "formatted": "https://signed-url.com"
  }
}