Show Menu
Cheatography

IBM Watson Cheat Sheet (DRAFT) by

IBM Watson CheatSheet

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

Get started

Visit https:­//c­lou­d.i­bm.com and create an account
create a resource and choose Visual Recogn­iti­onM­arkup in the AI category
use Lite Plan and copy your given API key and your URL
install Python Version 3
install these packages with pip: opencv­-py­thon/ ibm-watson

Face Recogn­ition code

import cv2
from ibm_watson import VisualRecognitionV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import json
import sys
 
authenticator = IAMAuthenticator('YOUR API KEY')
service = VisualRecognitionV3(
    version='2020-02-26',
    authenticator=authenticator
)
service.set_service_url('YOUR URL')
 
cap = cv2.VideoCapture(0)
 
key = ''
while( key != 'q' ):
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
 intkey = cv2.waitKey(2)
    if( intkey > 0 ):
        key = chr(intkey)
    else:
        key = ''
 
    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))
 

IBM Watson in Java

 IamOptions iamoptions = new IamOptions.Builder().apiKey("YOUR API KEY").build();

            SpeechToText stt = new SpeechToText(iamoptions);
            stt.setEndPoint("https://stream-fra.watsonplatform.net/speech-to-text/api");

            File audio = new File("src/resources/sample1.wav");

            RecognizeOptions options = new RecognizeOptions.Builder()
                    .audio(audio)
                    .contentType(HttpMediaType.AUDIO_WAV)
                    .build();

            SpeechRecognitionResults transcript = stt.recognize(options).execute();

            System.out.println(transcript);

            taText.setText(transcript.getResults().get(0).getAlternatives().get(0).getTranscript());

Speech to Text in Python

$ pip install --upgrade watson-developer-cloud

from speech_sentiment_python.recorder import Recorder

recorder = Recorder(“speech.wav”)
recorder.record_to_file()

def transcribe_audio(path_to_audio_file):
   username = os.environ.get(“USERNAME”)
   password = os.environ.get(“PASSWORD”)
   speech_to_text = SpeechToText(username=username,
   password=password)
   with open(join(dirname(__file__), path_to_audio_file), ‘rb’) as
   audio_file:
       return speech_to_text.recognize(audio_file,
                                       content_type=’audio/wav’)