#OpenCV – Access a local camera 🀳in F# πŸ“

Hi !

NET is in anniversary mode, and in Dev.to blog, someone asked about these samples in F#. It’s been a while since I wrote some F# code, and this is the perfect excuse to start.

Today Post: Access a local camera and show the camera frames in an OpenCV window. In following posts we will have a face detection demo in F#, like this one:

face blur demo in csharp using dnn

Easy to implement, and also easy to read:

Code Sample

// Copyright (c) 2022
// Author : Bruno Capuano
// Create Time : 2022 Feb
// Change Log :
// – Open a camera feed from a local USB webcam
// – Press [Q] to quit the app
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
open OpenCvSharp
let capture = new VideoCapture(0)
let window = new Window("El Bruno – F# Camera demo")
let image = new Mat()
let mutable run = true;
while (run = true) do
capture.Read(image)
if (image.Empty()) then
run <- false
window.ShowImage(image);
if (Cv2.WaitKey(1) = 113) then // Q
run <- false
view raw opencvcamera.fs hosted with ❤ by GitHub

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.