This is a draft cheat sheet. It is a work in progress and is not finished yet.
Account creation
Create an account -> "Create Resource" -> "AI" -> "Visual Recognition" -> "Lite Plan" -> copy API key and url |
Watson assistant
from ibm_watson import AssistantV2
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
//auth and set url
authenticator = IAMAuthenticator('{API_KEY}')
assistant = AssistantV2(
version='{version}',
authenticator=authenticator
)
assistant.set_service_url('{URL}')
//login with assistant id from webpage and receive sessionid
session = assistant.create_session(
assistant_id='{ASSISTANT_ID}'
).get_result()["session_id"]
//send messag using assistantid and sessionid
response_msg = assistant.message(
assistant_id='{ASSISTANT_ID}',
session_id='session',
input={
'message_type': 'text',
'text': 'Hello'
}
).get_result()
//print response
print(json.dumps(response, indent=2)) |
|
|
Install dependencies
pip3 install opencv-python
pip3 install ibm-watson |
|
|
Create python script
import cv2
from ibm_watson import VisualRecognitionV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import json
import sys
//auth with key and set url
authenticator = IAMAuthenticator('{API_KEY}')
service = VisualRecognitionV3(
version='2020-02-26',
authenticator=authenticator
)
service.set_service_url('{URL}')
cap = cv2.VideoCapture(0)
//read video from opencv till q is pressed
key = ''
while( key != 'q' ):
ret, frame = cap.read()
cv2.imshow('frame', frame)
intkey = cv2.waitKey(2)
if( intkey > 0 ):
key = chr(intkey)
else:
key = ''
//upload image to ibm and they'll classify it
if key == 'd' :
cv2.imwrite('upload.jpg', frame)
with open('upload.jpg','rb') as file:
classes = service.classify(images_file=file,threshold='0.6').get_result()
print(json.dumps(classes, indent=2)) |
|