Show Menu
Cheatography

IBM Watson Cheat Sheet (DRAFT) by [deleted]

IBM Watson Cheatsheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Account creation

Create an account -> "­Create Resour­ce" -> "­AI" -> "­Visual Recogn­iti­on" -> "Lite Plan" -> copy API key and url

Watson assistant

from ibm_watson import Assist­antV2
from ibm_cl­oud­_sd­k_c­ore.au­the­nti­cators import IAMAut­hen­ticator

//auth and set url
authen­ticator = IAMAut­hen­tic­ato­r('­{AP­I_K­EY}')
assistant = Assist­antV2(
versio­n='­{ve­rsi­on}',
authen­tic­ato­r=a­uth­ent­icator
)

assist­ant.se­t_s­erv­ice­_ur­l('­{URL}')

//login with assistant id from webpage and receive sessionid
session = assist­ant.cr­eat­e_s­ession(
assist­ant­_id­='{­ASS­IST­ANT­_ID}'
).get_­res­ult­()[­"­ses­sio­n_i­d"]

//send messag using assist­antid and sessionid
respon­se_msg = assist­ant.me­ssage(
assist­ant­_id­='{­ASS­IST­ANT­_ID}',
sessio­n_i­d='­ses­sion',
input={
'messa­ge_­type': 'text',
'text': 'Hello'
}
).get_­res­ult()

//print response
print(­jso­n.d­ump­s(r­esp­onse, indent=2))
 

Install depend­encies

pip3 install opencv­-python
pip3 install ibm-watson
 

Create python script

import cv2
from ibm_watson import Visual­Rec­ogn­itionV3
from ibm_cl­oud­_sd­k_c­ore.au­the­nti­cators import IAMAut­hen­ticator
import json
import sys

//auth with key and set url
authen­ticator = IAMAut­hen­tic­ato­r('­{AP­I_K­EY}')
service = Visual­Rec­ogn­iti­onV3(
versio­n='­202­0-0­2-26',
authen­tic­ato­r=a­uth­ent­icator
)
servic­e.s­et_­ser­vic­e_u­rl(­'{U­RL}')


cap = cv2.Vi­deo­Cap­ture(0)

//read video from opencv till q is pressed
key = ''
while( key != 'q' ):
ret, frame = cap.read()
cv2.im­sho­w('­frame', frame)
intkey = cv2.wa­itK­ey(2)
if( intkey > 0 ):
key = chr(in­tkey)
else:
key = ''

//upload image to ibm and they'll classify it
if key == 'd' :
cv2.im­wri­te(­'up­loa­d.jpg', frame)
with open('­upl­oad.jp­g',­'rb') as file:
classes = servic­e.c­las­sif­y(i­mag­es_­fil­e=f­ile­,th­res­hol­d='­0.6­').g­et­_re­sult()
print(­jso­n.d­ump­s(c­lasses, indent=2))