Caption Generator
Draw AI-generated Caption onto an Image
This flow uses the caption generator deep learning model from the IBM Model Asset Exchange to generate captions and image utils to render the caption on the image.
Requirements
[{"id":"c3d28aa.f84ff78","type":"subflow","name":"caption","info":"","category":"","in":[{"x":20,"y":260,"wires":[{"id":"6bae66fa.889ec8"},{"id":"e76f90cc.53908"}]}],"out":[{"x":2080,"y":260,"wires":[{"id":"fa9eb8c1.aa1258","port":0}]}],"env":[],"color":"#DDAA99"},{"id":"981f6ae5.448da8","type":"jimp-image","z":"c3d28aa.f84ff78","name":"","data":"payload","dataType":"msg","ret":"img","parameter1":"FONT_SANS_64_BLACK","parameter1Type":"jimpFont","parameter2":"20","parameter2Type":"num","parameter3":"20","parameter3Type":"num","parameter4":"subtitle","parameter4Type":"msg","parameter5":"HORIZONTAL_ALIGN_CENTER","parameter5Type":"AlignX","parameter6":"VERTICAL_ALIGN_TOP","parameter6Type":"AlignY","parameter7":"","parameter7Type":"auto","parameter8":"","parameter8Type":"auto","parameterCount":8,"jimpFunction":"print2","selectedJimpFunction":{"name":"print aligned","fn":"print","description":"Print text to the image","parameters":[{"name":"font|str","type":"jimpFont","required":true,"hint":"font to print. NOTE: This can be one of the presets or the path to a fnt file"},{"name":"x","type":"num","required":true,"hint":"x coordinate to print text"},{"name":"y","type":"num","required":true,"hint":"y coordinate to print text"},{"name":"text","group":"options","type":"str","required":true,"hint":"text to print"},{"name":"alignmentX","group":"options","type":"AlignX","required":false,"hint":"X Alignment"},{"name":"alignmentY","group":"options","type":"AlignY","required":false,"hint":"Y Alignment"},{"name":"maxWidth","type":"auto|num","required":false,"hint":"wrap text at maxWidth"},{"name":"maxHeight","type":"auto|num","required":false,"hint":""}]},"x":1270,"y":340,"wires":[["97efa787.d72f88"]]},{"id":"6bae66fa.889ec8","type":"change","z":"c3d28aa.f84ff78","name":"get image info","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"w\": msg.imageInfo.width,\"h\":msg.imageInfo.height,\"background\":\"#fff\"}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":180,"wires":[["ce3962b1.891e38"]]},{"id":"79814249.d4d81c","type":"jimp-image","z":"c3d28aa.f84ff78","name":"blank banner","data":"payload","dataType":"msg","ret":"img","parameter1":"","parameter1Type":"msg","parameter2":"","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","parameterCount":0,"jimpFunction":"none","selectedJimpFunction":{"name":"none","fn":"none","description":"Just loads the image.","parameters":[]},"x":1090,"y":340,"wires":[["981f6ae5.448da8"]]},{"id":"476e7da.c801c04","type":"join","z":"c3d28aa.f84ff78","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1770,"y":260,"wires":[["fa9eb8c1.aa1258"]]},{"id":"97efa787.d72f88","type":"change","z":"c3d28aa.f84ff78","name":"topic: banner","rules":[{"t":"set","p":"topic","pt":"msg","to":"banner","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1470,"y":340,"wires":[["476e7da.c801c04"]]},{"id":"e76f90cc.53908","type":"change","z":"c3d28aa.f84ff78","name":"topic: image","rules":[{"t":"set","p":"topic","pt":"msg","to":"image","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1470,"y":260,"wires":[["476e7da.c801c04"]]},{"id":"fa9eb8c1.aa1258","type":"jimp-image","z":"c3d28aa.f84ff78","name":"","data":"payload.image","dataType":"msg","ret":"img","parameter1":"payload.banner","parameter1Type":"msg","parameter2":"0","parameter2Type":"num","parameter3":"payload.position","parameter3Type":"msg","parameter4":"BLEND_SOURCE_OVER","parameter4Type":"Blend","parameter5":"0.5","parameter5Type":"num","parameter6":"0.5","parameter6Type":"num","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","parameterCount":6,"jimpFunction":"composite","selectedJimpFunction":{"name":"composite","fn":"composite","description":"composites another Jimp image over this image at x, y","parameters":[{"name":"src","type":"","required":true,"hint":"the source Jimp instance","defaultType":"msg","defaultValue":"payload"},{"name":"x","type":"num","required":true,"hint":"the x position to blit the image"},{"name":"y","type":"num","required":true,"hint":"the y position to blit the image"},{"name":"mode","group":"options","type":"blend","required":true,"hint":"what blend mode to use"},{"name":"opacitySource","group":"options","type":"num","required":true,"hint":"opacity of src image 0.0 to 1.0"},{"name":"opacityDest","group":"options","type":"num","required":true,"hint":"opacity of src image 0.0 to 1.0"}]},"x":1930,"y":260,"wires":[[]]},{"id":"14d8fabf.b8b8ed","type":"change","z":"c3d28aa.f84ff78","name":"topic: position","rules":[{"t":"set","p":"topic","pt":"msg","to":"position","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1480,"y":180,"wires":[["476e7da.c801c04"]]},{"id":"b030f91.33fe788","type":"change","z":"c3d28aa.f84ff78","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.h - bannerHeight","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":180,"wires":[["14d8fabf.b8b8ed"]]},{"id":"9e8749f5.0b05f8","type":"change","z":"c3d28aa.f84ff78","name":"","rules":[{"t":"set","p":"payload.h","pt":"msg","to":"bannerHeight","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":340,"wires":[["79814249.d4d81c"]]},{"id":"ce3962b1.891e38","type":"change","z":"c3d28aa.f84ff78","name":"","rules":[{"t":"set","p":"bannerHeight","pt":"msg","to":"180","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":180,"wires":[["b030f91.33fe788","9e8749f5.0b05f8"]]},{"id":"87c2eff7.aaa93","type":"tab","label":"Caption Generator","disabled":false,"info":"This flow uses the https://developer.ibm.com/exchanges/models/all/max-image-caption-generator/ deep learning model from the Model Asset Exchange to generate captions that describe the content of the input image. Refer to the documentation for information about the returned message.\n\nInstall the following two modules to run this example flow:\n - [node-red-contrib-model-asset-exchange](https://www.npmjs.com/package/node-red-contrib-model-asset-exchange)\n - [node-red-contrib-image-utils](https://www.npmjs.com/package/node-red-contrib-image-tools)\n \n \n> Note: The image-caption-generator node has been pre-configured to use a hosted model evaluation instance. We recommend using your own local or cloud instance for purposes other than evaluation."},{"id":"3c28073a.87f28","type":"image-caption-generator","z":"87c2eff7.aaa93","service":"30951a65.bd213e","method":"predict","passthrough":true,"predict_body":"","predict_bodyType":"str","name":"","x":1150,"y":740,"wires":[["54cdd00e.a2d018"]]},{"id":"98c4858e.eb95e8","type":"comment","z":"87c2eff7.aaa93","name":"download image","info":"","x":700,"y":60,"wires":[]},{"id":"ce75b46b.71d08","type":"http request","z":"87c2eff7.aaa93","name":"","method":"GET","ret":"bin","paytoqs":false,"url":"{{{payload}}}","tls":"","persist":false,"proxy":"","authType":"","x":870,"y":100,"wires":[["3c28073a.87f28","7033ecb0.3e1514"]]},{"id":"abf03c15.c51678","type":"inject","z":"87c2eff7.aaa93","name":"image URL","topic":"","payload":"https://magazineen-ed3b.kxcdn.com/wp-content/uploads/2019/05/single-man-dog.jpg","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":700,"y":100,"wires":[["ce75b46b.71d08"]]},{"id":"7033ecb0.3e1514","type":"image viewer","z":"87c2eff7.aaa93","name":"","width":"400","data":"payload","dataType":"msg","x":1090,"y":100,"wires":[[]]},{"id":"6f250e6b.6670f8","type":"subflow:c3d28aa.f84ff78","z":"87c2eff7.aaa93","name":"caption","env":[],"x":1660,"y":740,"wires":[["99007870.03311"]]},{"id":"99007870.03311","type":"image viewer","z":"87c2eff7.aaa93","name":"","width":"400","data":"payload","dataType":"msg","x":1890,"y":100,"wires":[[]]},{"id":"4d610b99.97cefc","type":"comment","z":"87c2eff7.aaa93","name":"generate caption text","info":"","x":1140,"y":700,"wires":[]},{"id":"c3e461c5.766858","type":"comment","z":"87c2eff7.aaa93","name":"create banner","info":"","x":1650,"y":700,"wires":[]},{"id":"e89f9ac2.8a8bd8","type":"jimp-image","z":"87c2eff7.aaa93","name":"set payload","data":"inputData","dataType":"msg","ret":"img","parameter1":"","parameter1Type":"msg","parameter2":"","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","parameterCount":0,"jimpFunction":"none","selectedJimpFunction":{"name":"none","fn":"none","description":"Just loads the image.","parameters":[]},"x":1510,"y":740,"wires":[["6f250e6b.6670f8"]]},{"id":"54cdd00e.a2d018","type":"change","z":"87c2eff7.aaa93","name":"set subtitle","rules":[{"t":"set","p":"subtitle","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1350,"y":740,"wires":[["e89f9ac2.8a8bd8"]]},{"id":"9696e2da.2e221","type":"comment","z":"87c2eff7.aaa93","name":"display result","info":"","x":1910,"y":60,"wires":[]},{"id":"30951a65.bd213e","type":"image-caption-generator-service","z":"","host":"https://max-image-caption-generator.codait-prod-41208c73af8fca213512856c7a09db52-0000.us-east.containers.appdomain.cloud","name":"cloud"}]