Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pythonによる工業用カメラ画像取得事例

OHNO
August 08, 2020

 Pythonによる工業用カメラ画像取得事例

OHNO

August 08, 2020
Tweet

More Decks by OHNO

Other Decks in Programming

Transcript

  1. Pythonによる工業用カメラ
    画像取得事例
    TensorFlow User Group Niigata #3
    大野 宏

    View Slide

  2. 画像分類をするためには
    ・製造業で画像分類や傷を検査するためには、多くの高
    解像度の画像が必要
    ・PythonではUSBカメラの画像の取得は容易であるが、高
    解像度の工業用カメラは・・・
    ・メーカが提供するライブラリは、C、C#、C++
    ・画像取り込みはC、画像分類はPython
    → 同じ言語で画像取り込みから分類まで出来るとよい
    露光時間、フレームレート等を制御可能

    View Slide

  3. Basler 社のカメラ
    https://www.baslerweb.com/jp/products/software/basler-pylon-
    camera-software-suite/pylon-open-source-projects/

    View Slide

  4. Pythonライブラリ
    https://github.com/basler/pypylon

    View Slide

  5. PythonによるUSBカメラ画像取得
    ・画像処理ライブラリOpenCVを使ったプログラム
    import cv2
    capture = cv2.VideoCapture(1)
    while(True):
    ret, frame = capture.read()
    cv2.imshow('title',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
    break

    View Slide

  6. Pythonプログラム1/3
    from pypylon import pylon
    from pypylon import genicam
    import sys
    import cv2
    import numpy as np
    width = 2044
    height = 1536
    img1 = np.zeros((height, width, 1), np.uint8)
    countOfImagesToGrab = 10
    exitCode = 0
    try:
    camera =pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
    camera.Open()
    print("Using device ", camera.GetDeviceInfo().GetModelName())
    new_width = camera.Width.GetValue() - camera.Width.GetInc()

    View Slide

  7. Pythonプログラム2/3
    if new_width >= camera.Width.GetMin():
    camera.Width.SetValue(new_width)
    camera.MaxNumBuffer = 5
    while True:
    camera.StartGrabbingMax(1)
    while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(5000,
    pylon.TimeoutHandling_ThrowException)
    if grabResult.GrabSucceeded():
    img = grabResult.Array
    cv2.imshow("img",img)
    key=cv2.waitKey(1)
    else:
    print("Error: ", grabResult.ErrorCode, grabResult.ErrorDescription)
    if key=='q’:
    break
    grabResult.Release()

    View Slide

  8. Pythonプログラム3/3
    camera.Close()
    except genicam.GenericException as e:
    # Error handling.
    print("An exception occurred.")
    print(e.GetDescription())
    exitCode = 1
    sys.exit(exitCode)

    View Slide