TMWL July’20 — image detection and type safety

Programmers share what they’ve learned

Maria Kucharczyk
SoftwareMill Tech Blog

--

Every month we share what we’ve learned in our team. In July Michał and Tomasz discovered:

  • how to do simple object detection using neural networks,
  • how to generate TypeScript typings for REST APIs.

How to detect a cat in your garden? — by Michał

This month I’ve learned how to do simple object detection using neural networks. My problem is quite simple: cats visiting my garden are leaving behind unpleasant surprises 💩

I decided to do some experiments with video analysis. The simplest way to do that is to use pre-trained neural network, e.g. Yolo-v4 trained using Microsoft COCO dataset. It includes 80 different very common object categories and in overall 330k images. To get more info about the YoloV4 take a look at this blog and related paper.

How to run Yolo? You just have to compile the sources, download the network and provide the image/video file/video stream you would like to analyse. Video analysis is very resource-consuming and it requires GPU (CUDA) usage. How does it work in practice? Take a look at the sample video:

Yes, it can work in real-time. Apart from Yolo-v4 there is a second model available — Yolo-v4-tiny. It is designed for smaller devices like Raspberry Pi — it offers better performance, but lower accuracy.

I present the result of the experiment — a cat.

However it is not perfect, and depending on the image, the cat can become a dog, a person, a horse or even a vase, of course with lower accuracy.

In the future I can improve the accuracy by re-training the model based on gathered videos from my garden, however so far I don’t have enough data for that.

Generating TypeScript typings for REST APIs — by Tomasz

Recently, I’ve been a part of a project which consists of a JSON REST API (implemented using Scala & Tapir) and a Single Page Application browser client (implemented using TypeScript).

I like the type-safety on both the server and client sides that the languages provide. However, the communication between them is not statically typed. Errors can still happen at runtime if e.g. we reference a non-existent field from the response JSON, or forget to provide a required field in the request JSON body.

This got me thinking… Tapir already generates documentation for our API, using Swagger. This takes the form of a YAML file. If we could generate TypeScript typings from this API documentation, we would get compilation errors whenever the client uses the API incorrectly. Wouldn’t that be great?

Luckily, there’s a package called dtsgenerator that can do just that! Point it at your Swagger API definition and it will spit out TypeScript types representing JSON requests & responses. Here’s an example from a publicly hosted API:

and needs to be included your project’s tsconfig.json under “files” property:

This solution definitely isn’t 100% failure-proof. For example, it will only verify types of JSON bodies — you can still make a mistake by requesting a wrong HTTP path or using a wrong verb (e.g. PUT instead of POST). However, it still provides a lot of type safety, is very simple to use, and is compatible with any API that uses Swagger for documentation.

To see a complete example of a REST API implemented with Tapir and a TypeScript client, visit my github.

And what have you learned in July? Let us know! :)

BTW, we are always looking for outstanding professionals to join our team!

Questions? Ask us anything about remote work, how does the cooperation with us look like, what projects do we have, or about anything else - on the dedicated Slack channel 💡

--

--