Computer Vision Python Code Examples
Quicky integrate state of the art computer vision AI into your Python application with asticaVision REST API. The asticaVision API provides a seamless way to leverage powerful computer vision AI models and algorithms in your Python application.
Python developers can begin incorporating computer vision into their photography, robotics, augmented reality projects. Vision AI can be used for exploring the capabilities of visual data analysis, computer vision can add a new level of fun and creativity to your projects. With Python, hobbyist developers can dive into the world of computer vision and enjoy improving their efficiency, experimenting with quality control, enhancing safety measures, and creating personalized experiences while keeping privacy and ethical considerations in mind.
Try Online1. Advanced Functionality:
Use python to perform powerful computer vision on any python powered device, even low powered IoT such as the Raspberry Pi.
- Detailed descriptive vision.
- Person Detection: Including age, and gender.
- OCR: Read written and text.
- Object Detection: Including brand or species
- Detect adult content and mature subject matter.
- Automatically sort, tag, and describe images and documents.
2. Simple Integration:
Integrating asticaVision into your web and mobile applications using Python is simple and can be done with only a few lines of code. This easy but powerful solution will quickly transform your applications into advanced image analysis and understanding tools.
3. Powerful and Accessible Vision AI
Computer vision is an exciting technology that can be utilized in various hobbyist domains.
Python developers can begin incorporating computer vision into their photography, robotics, augmented reality projects. It can be just exploring the capabilities of visual data analysis, computer vision can add a new level of fun and creativity to your projects. With Python, hobbyist developers can dive into the world of computer vision and enjoy improving their efficiency, experimenting with quality control, enhancing safety measures, and creating personalized experiences while keeping privacy and ethical considerations in mind.
Here is an example code demonstrating basic use of computer vision with javascript.
# base64 or url (https) of image asticaAPI_input = 'https://astica.ai/example/asticaVision_sample.jpg';
# comma separated list of parameters
asticaAPI_visionParams = 'gpt, describe, describe_all, objects, faces';
# If using "gpt" or "gpt_detailed" visionParam,
# optional custom prompt and length
# describe the image, answer the math question, etc
asticaAPI_gpt_prompt = '';
asticaAPI_prompt_length = 95;
# If using "objects_custom" visionParam,
# supply a list of comma-separated keywords
asticaAPI_objects_custom_kw = '';
# Set Model Version
asticaAPI_modelVersion = '2.5_full';
# set your key
asticaAPI_key = 'YOUR API KEY'; # generate: https://astica.ai/api-keys/
# Define CURL Timeout
asticaAPI_timeout = 20; //seconds
# Set vision AI endpoint asticaAPI_endpoint = 'https://vision.astica.ai/describe';
# Build Model Input
asticaAPI_payload = {
'tkn': asticaAPI_key,
'modelVersion': asticaAPI_modelVersion,
'input': asticaAPI_input,
'visionParams': asticaAPI_visionParams,
'gpt_prompt': asticaAPI_gpt_prompt,
'prompt_length': asticaAPI_prompt_length
}
# Perform Python Computer Vision
response = requests.post(endpoint, data=json.dumps(asticaAPI_payload), timeout=timeout, headers={ 'Content-Type': 'application/json', })
if response.status_code == 200:
print(response.json())
else:
print('Failed to connect to the API.')
This python script performs computer vision on either an image URL or base64 input. When ran, the code will display the output for all of the provided parameters. Harnessing the power of Python, you can seamlessly identify objects and faces or generate insightful image captions. You can continue reading for a larger scripts which prints each computer vision parameter output.
View Full API ParametersNotice:
- If a website blocks hotlinking, any request with a URL input will result in a failed response.
The code found below is ready to run on any computer with python installed.
Analyze an Image with Python
This is complete script which demonstrates how to make a request to asticaVision using Python. You can specify the HTTPS URL of the image input, or you can provide a Base64 encoded string. You can uncomment the sample code to send a base64 input.
import requests import json import base64 import os def get_image_base64_encoding(image_path: str) -> str: with open(image_path, 'rb') as file: image_data = file.read() image_extension = os.path.splitext(image_path)[1] return base64.b64encode(image_data).decode('utf-8') # API configurations asticaAPI_key = 'YOUR API KEY' # visit https://astica.ai asticaAPI_timeout = 25 # in seconds. "gpt" or "gpt_detailed" require increased timeouts asticaAPI_endpoint = 'https://vision.astica.ai/describe' asticaAPI_modelVersion = '2.5_full' # 1.0_full, 2.0_full, 2.1_full or 2.5_full if 1 == 1: asticaAPI_input = 'https://astica.ai/example/asticaVision_sample.jpg' # use https image input (faster) else: asticaAPI_input = get_image_base64_encoding('image.jpg') # use base64 image input (slower) # vision parameters: https://astica.ai/vision/documentation/#parameters asticaAPI_visionParams = 'gpt,describe,objects,faces' # comma separated, defaults to "all". ''' '2.5_full' supported visionParams: https://astica.ai/vision/documentation/#parameters describe describe_all gpt (or) gpt_detailed text_read objects objects_custom objects_color categories moderate tags color faces celebrities landmarks brands ''' #optional inputs: asticaAPI_gpt_prompt = '' # only used if visionParams includes "gpt" or "gpt_detailed" asticaAPI_prompt_length = 90 # number of words in GPT response asticaAPI_objects_custom_kw = '' # only used if visionParams includes "objects_custom" (v2.5_full or higher) # Define payload dictionary asticaAPI_payload = { 'tkn': asticaAPI_key, 'modelVersion': asticaAPI_modelVersion, 'visionParams': asticaAPI_visionParams, 'input': asticaAPI_input, 'gpt_prompt': asticaAPI_gpt_prompt, 'prompt_length': asticaAPI_prompt_length, 'objects_custom_kw': asticaAPI_objects_custom_kw } def asticaAPI(endpoint, payload, timeout): response = requests.post(endpoint, data=json.dumps(payload), timeout=timeout, headers={ 'Content-Type': 'application/json', }) if response.status_code == 200: return response.json() else: return {'status': 'error', 'error': 'Failed to connect to the API.'} # call API function asticaAPI_result = asticaAPI(asticaAPI_endpoint, asticaAPI_payload, asticaAPI_timeout) # print API output print('\nastica API Output:') print(json.dumps(asticaAPI_result, indent=4)) print('=================') print('=================') # Handle asticaAPI response if 'status' in asticaAPI_result: # Output Error if exists if asticaAPI_result['status'] == 'error': print('Output:\n', asticaAPI_result['error']) # Output Success if exists if asticaAPI_result['status'] == 'success': if 'caption_GPTS' in asticaAPI_result and asticaAPI_result['caption_GPTS'] != '': print('=================') print('GPT Caption:', asticaAPI_result['caption_GPTS']) if 'caption' in asticaAPI_result and asticaAPI_result['caption']['text'] != '': print('=================') print('Caption:', asticaAPI_result['caption']['text']) if 'CaptionDetailed' in asticaAPI_result and asticaAPI_result['CaptionDetailed']['text'] != '': print('=================') print('CaptionDetailed:', asticaAPI_result['CaptionDetailed']['text']) if 'objects' in asticaAPI_result: print('=================') print('Objects:', asticaAPI_result['objects']) else: print('Invalid response')
This python code can be saved to your local computer with python installed. Change the API key variable with one that was generated by your account and run the script to view the results of the computer vision model.
The example demonstrates how to use vision AI with an external image hosted at a https URL, as well as sending a local file as a base-64 encoded string. Please note that sending base64 inputs is less affordable due to bandwidth/ingress cost.
Discover More AI
Experiment with different kinds of artificial intelligence. See, hear, and speak with astica.