opencv打开摄像头_利用USB摄像头,免费快速搭建浏览器远程监控

用OpenCV实现桌面的摄像头程序很简单,把这部分代码集成到一个简单的HTTP server上就可以实现浏览器远程监控。

OpenCV安装

我这里使用了opencv4nodejs:

npm i opencv4nodejs 

安装的时间会有点长,需要先下载OpenCV源码再编译。如果发现编译不通过,请阅读错误信息,再检查系统是否安装了需要的工具。

简单的Node.js桌面摄像头应用

创建一个desktop.js文件:

const cv = require('opencv4nodejs');
 
const vCap = new cv.VideoCapture(0);
  
const delay = 10;
while (true) {
  let frame = vCap.read();
  if (frame.empty) {
    vCap.reset();
    frame = vCap.read();
  }
  
  cv.imshow('OpenCV Node.js', frame);
  const key = cv.waitKey(delay); // Press ESC to quit
  if (key == 27) {break;}
}

运行程序:

node desktop.js 

<br> (二维码自动识别)

通过浏览器访问摄像头

原理

  1. 启动一个简单的web服务,并不断获取摄像头数据
  2. 把数据通过HTTP请求发送到web客户端的image元素用于显示
  3. web客户端通过setTimeout不断请求服务器刷新image图像

实现方法

创建一个包含image元素的HTML文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Webcam</title>
</head>

<body>
    
    <img id="image"/>

    <script type="text/javascript">
        var image = document.getElementById('image');
        function refresh() {
            image.src = "/image?" + new Date().getTime();
            image.onload= function(){
                setTimeout(refresh, 30);
            }
        }
        
        refresh();
    </script>

</body>
</html>

然后创建一个web.js文件:

const http = require('http'); 
const cv = require('opencv4nodejs')
const wCap = new cv.VideoCapture(0);
wCap.set(cv.CAP_PROP_FRAME_WIDTH, 640);
wCap.set(cv.CAP_PROP_FRAME_HEIGHT, 480);

var img = null;
function capture() {
    var frame = wCap.read()
    if (frame.empty) {
        wCap.reset();
        frame = wCap.read();
    }

    img = cv.imencode('.jpg', frame);
    setTimeout(capture, 30);
}

capture();

var fs=require("fs");
var html = fs.readFileSync("index.htm", "utf8");

var server = http.createServer(function (req, res) {   
    if (req.url === '/' || req.url === '/index.htm') { 
        
        res.writeHead(200, { 'Content-Type': 'text/html' });   
        res.write(html);
        res.end();
    
    }
    else if (req.url.startsWith("/image")) {
        
        res.writeHead(200, { 'Content-Type': 'image/jpeg' });
        res.write(img);
        res.end();
    
    }
    else
        res.end('Invalid Request!');

});

server.listen(2020);

console.log('Node.js web server is running on port 2020...')

运行web.js:

node web.js 

现在可以通过任意浏览器打开localhost:2020,看到视频。因为只用了image元素,所以即使用IE打开也没有任何兼容问题。

9bfac55105a6ff427162460641e4af12.png

在手机上输入IP地址查看页面。

<br> (二维码自动识别)

局域网映射工具

内网启动服务之后,借助免费的工具把端口映射到公网。以下工具都有免费版,可以生成一个临时的网址。

  • Ngrok (国内访问比较慢)
  • 花生壳 (虽然慢,但还过得去)

4e3200439972f886d374ea54cfa502f7.png

现在可以在任何地方访问我办公室的摄像头了。在微信中打开监控:

6075a55e22e17354af444ba72a35eef2.png

视频

知乎视频​www.zhihu.com

源码

https://github.com/yushulx/opencv-webcam

好的,要使用Java OpenCV调用摄像头并进行文字识别,您需要遵循以下步骤: 1. 安装Tesseract OCR引擎 Tesseract OCR引擎是一个开源的OCR引擎,可以识别多种语言的文本。您可以从Tesseract OCR官方网站下载并安装Tesseract OCR引擎。 2. 导入Tesseract OCR库 在Java项目中,您需要导入Tesseract OCR库。您可以使用Maven和Gradle等构建工具来导入Tesseract OCR库。 3. 调用摄像头 您可以使用Java OpenCV中的VideoCapture类调用摄像头。以下是一个简单的示例代码: ``` import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.videoio.VideoCapture; public class CameraCapture { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); VideoCapture camera = new VideoCapture(0); if(!camera.isOpened()){ System.out.println("Error"); } else { Mat frame = new Mat(); while(true){ if (camera.read(frame)){ System.out.println("Frame Obtained"); System.out.println("Captured Frame Width " + frame.width() + " Height " + frame.height()); Core.flip(frame, frame, 1); break; } } } camera.release(); } } ``` 此代码段将获取摄像头获取的帧并将其翻转。 4. 文字识别 要进行文字识别,您可以使用Java OpenCV中的Imgcodecs类将帧转换为图像,然后使用Tesseract OCR库进行识别。以下是一个简单的示例代码: ``` import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.videoio.VideoCapture; import net.sourceforge.tess4j.*; public class TextRecognition { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); VideoCapture camera = new VideoCapture(0); Mat frame = new Mat(); while (true){ if (camera.read(frame)){ Mat grayFrame = new Mat(); Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY); File imageFile = new File("image.png"); Imgcodecs.imwrite(imageFile.getAbsolutePath(), grayFrame); ITesseract tess = new Tesseract(); tess.setDatapath("tessdata"); tess.setLanguage("eng"); String result = tess.doOCR(imageFile); System.out.println(result); HighGui.imshow("Text Recognition", frame); HighGui.waitKey(1); } } } } ``` 此代码段将在从摄像头获取的帧中识别文本。它首先将帧转换为灰度图像,然后将其保存到磁盘上的图像文件中。然后,它使用Tesseract OCR库对图像文件中的文本进行识别,并将结果打印到控制台上。您需要将Tesseract OCR库的数据路径设置为“tessdata”文件夹,其中包含识别语言的数据文件。 希望这可以帮助您开始使用Java OpenCV调用摄像头并进行文字识别
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值