捕获图像-虚拟物联网硬件


文档摘要

捕获图像 - 虚拟物联网硬件 在本课程的这一部分,你将为你的虚拟物联网设备添加一个摄像头传感器,并从该摄像头读取图像。 硬件 虚拟物联网设备将使用模拟摄像头发送文件中的图像或你的网络摄像头中的图像。 将摄像头添加到CounterFit 要使用虚拟摄像头,你需要将其添加到CounterFit应用中。 任务 - 将摄像头添加到CounterFit 将摄像头添加到CounterFit应用中。 在你的计算机上创建一个新的Python应用,文件夹名为 ,其中包含一个名为 的单个文件和一个Python虚拟环境,并添加CounterFit pip包。 ⚠️ 如果需要,可以参考第1课中创建和设置CounterFit Python项目的说明。

捕获图像 - 虚拟物联网硬件

在本课程的这一部分,你将为你的虚拟物联网设备添加一个摄像头传感器,并从该摄像头读取图像。

硬件

虚拟物联网设备将使用模拟摄像头发送文件中的图像或你的网络摄像头中的图像。

将摄像头添加到CounterFit

要使用虚拟摄像头,你需要将其添加到CounterFit应用中。

任务 - 将摄像头添加到CounterFit

将摄像头添加到CounterFit应用中。

  1. 在你的计算机上创建一个新的Python应用,文件夹名为fruit-quality-detector,其中包含一个名为app.py的单个文件和一个Python虚拟环境,并添加CounterFit pip包。

    ⚠️ 如果需要,可以参考第1课中创建和设置CounterFit Python项目的说明

  2. 安装一个额外的Pip包来安装一个CounterFit适配器,该适配器可以通过模拟一些Picamera Pip包来与摄像头传感器通信。确保你在激活虚拟环境的终端中进行此操作。

    pip install counterfit-shims-picamera
  3. 确保CounterFit网页应用正在运行。

  4. 创建一个摄像头:

    1. 在“传感器”窗格的“创建传感器”框中,展开“传感器类型”框并选择“摄像头”。

    2. 将“名称”设置为Picamera

    3. 选择“添加”按钮以创建摄像头。

    摄像头设置

    摄像头将被创建并出现在传感器列表中。

    已创建的摄像头

编程摄像头

现在可以编程虚拟物联网设备以使用虚拟摄像头了。

任务 - 编程摄像头

对设备进行编程。

  1. 确保fruit-quality-detector应用在VS Code中打开。

  2. 打开app.py文件。

  3. app.py顶部添加以下代码以连接应用到CounterFit:

    from counterfit_connection import CounterFitConnection CounterFitConnection.init('127.0.0.1', 5000)
  4. app.py文件中添加以下代码:

    import io from counterfit_shims_picamera import PiCamera

    这段代码导入了一些库,包括来自counterfit_shims_picamera库的PiCamera类。

  5. 在这之后添加以下代码以初始化摄像头:

    camera = PiCamera() camera.resolution = (640, 480) camera.rotation = 0

    这段代码创建了一个PiCamera对象,将分辨率设置为640x480。虽然支持更高的分辨率,但图像分类器在较小的图像(227x227)上工作,因此无需捕获和发送更大的图像。

    camera.rotation = 0 line sets the rotation of the image in degrees. If you need to rotate the image from the webcam or the file, set this as appropriate. For example, if you want to change the image of a banana on a webcam in landscape mode to be portrait, set camera.rotation = 90

  6. 在这之后添加以下代码以将图像捕获为二进制数据:

    image = io.BytesIO() camera.capture(image, 'jpeg') image.seek(0)

    这段代码创建了一个BytesIO object to store binary data. The image is read from the camera as a JPEG file and stored in this object. This object has a position indicator to know where it is in the data so that more data can be written to the end if needed, so the image.seek(0)行将位置移回到开始,以便稍后可以读取所有数据。

  7. 在这之下添加以下内容以将图像保存到文件:

    with open(https://www.aiknowledge.cn/images/初学者的IOT开发课程/'image.webp) as image_file: image_file.write(image.read())

https://www.aiknowledge.cn/images/初学者的IOT开发课程/ 这段代码打开一个名为`image.webp

> You can capture the image directly to a file instead of a `BytesIO` object by passing the file name to the `camera.capture` call. The reason for using the `BytesIO` object is so that later in this lesson you can send the image to your image classifier.
  1. Configure the image that the camera in CounterFit will capture. You can either set the Source to File, then upload an image file, or set the Source to WebCam, and images will be captured from your web cam. Make sure you select the Set button after selecting a picture or selecting your webcam.

    CounterFit with a file set as the image source, and a web cam set showing a person holding a banana in a preview of the webcam

https://www.aiknowledge.cn/images/初学者的IOT开发课程/1. An image will be captured and saved as `image.webp

你可以在code-camera/virtual-iot-device文件夹中找到这段代码。

你的摄像头程序成功了!

声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。


发布者: 作者: 转发
评论区 (0)
U