<jupyter_start><jupyter_text>Deep learning: vision<jupyter_code>#load "Paket.fsx"
Paket.Version [ "FSharp.Data", "3.0"]
#load "Paket.Generated.Refs.fsx"
open System.IO  
open FSharp.Data
open FSharp.Data.HttpRequestHeaders
open FSharp.Data.HttpContentTypes
 
//#load "subscription.fs" // Load subscription key

module Subscription
    let subscriptionKey = ""  // TODO: Supply your Azure API key for cognitive services (vision)

let uriBase = "https://westeurope.api.cognitive.microsoft.com/vision/v2.0/analyze"

type VisionApi = 
  JsonProvider<"""{"tags":[{"name":"music","confidence":0.98865640163421631},{"name":"guitar","confidence":0.95694917440414429},{"name":"building material","confidence":0.41293439269065857},{"name":"lumber","confidence":0.29446199536323547},{"name":"bass","confidence":0.26204556226730347,"hint":"object"}],"description":{"tags":["road","building","outdoor","street","night","black","city","white","light","sitting","riding","man","side","empty","rain","corner","traffic","lit","hydrant","stop","board","parked","bus","tall"],"captions":[{"text":"a close up of an empty city street at night","confidence":0.7965622853462756}]},"requestId":"ad111816-5803-4176-a3a0-0aa59b517db6","metadata":{"width":604,"height":271,"format":"Jpeg"}}""">

let getPictureDescription imageFilePath = 
    let bytes = File.ReadAllBytes imageFilePath
    let requestParameters = "visualFeatures=Tags,Description"
    let res = 
        Http.RequestString(uriBase + "?" + requestParameters, 
                           headers = [ "Ocp-Apim-Subscription-Key", Subscription.subscriptionKey
                                       ContentType Binary ],
                           body = HttpRequestBody.BinaryUpload bytes)
    let jsonObject = VisionApi.Parse res
    
    let description = jsonObject.Description.Captions.[0].Text
    let tags = jsonObject.Tags |> Array.map (fun t -> t.Name, t.Confidence)
    
    printfn "%s\n------------------------------" description
    tags |> Array.iter (fun (name, p) -> printfn "%s: %.3f" name p)<jupyter_output><empty_output><jupyter_text>![](img/guitar.jpg)<jupyter_code>getPictureDescription "img/guitar.jpg"<jupyter_output><empty_output><jupyter_text>![](img/monkey1.jpeg)<jupyter_code>getPictureDescription "img/monkey1.jpeg"<jupyter_output><empty_output><jupyter_text>![](img/monkey2.jpg)<jupyter_code>getPictureDescription "img/monkey2.jpg"<jupyter_output><empty_output><jupyter_text>Out of sample example ![](img/smeal.png)<jupyter_code>getPictureDescription "img/smeal.png"<jupyter_output><empty_output><jupyter_text>Simple image modifications ![](img/gmonkey2.jpeg)<jupyter_code>getPictureDescription "img/gmonkey2.jpeg"<jupyter_output><empty_output><jupyter_text>![](img/gmonkey1.jpeg)<jupyter_code>getPictureDescription "img/gmonkey1.jpeg"<jupyter_output><empty_output><jupyter_text>![](img/ggmonkey2.jpeg)<jupyter_code>getPictureDescription "img/ggmonkey2.jpeg"<jupyter_output><empty_output><jupyter_text>What does it see? ![](img/landscape1.jpg)source: `aiweirdness.com`<jupyter_code>getPictureDescription "img/landscape1.jpg"<jupyter_output><empty_output><jupyter_text>![](img/office1.jpg)<jupyter_code>getPictureDescription "img/office1.jpg"<jupyter_output><empty_output><jupyter_text>![](img/office1elephant.jpg)<jupyter_code>getPictureDescription "img/office1elephant.jpg"<jupyter_output><empty_output><jupyter_text>![](img/monkey2elephant.jpeg)<jupyter_code>getPictureDescription "img/monkey2elephant.jpeg"<jupyter_output><empty_output><jupyter_text>AI Surrealism![](img/magritte.jpg)<jupyter_code>getPictureDescription "img/magritte.jpg"<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1696453120703877, 'fraction_numerical': 0.04399230134726423, 'mean_word_length': 7.013215859030837, 'pattern_counts': {'":': 22, '<': 54, '<?xml version=': 0, '>': 58, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '451800', 'license': 'MIT', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1226, 'n_words': 127, 'path': 'deep-learning/VisionDemo.ipynb', 'repo_name': 'evelinag/breaking-black-box-ai', 'size': '3637'}