樹莓派4b開發之開發環境搭建


工作需要購置樹莓派4b板子兩塊,現記錄一下搭建開發環境的過程

一、樹莓派系統鏡像燒寫

  1、官網下載最新系統鏡像,我下的文件名為:2020-08-20-raspios-buster-armhf.img

  2、准備一張16G的內存卡,使用專門的格式化工具SDFormatter格式化SD卡。

  3、Windows系統下下載安裝燒寫鏡像工具Win32DiskImager。

      

 

    (1)選擇下載的系統鏡像文件

    (2)'設備'選擇SD卡盤符.

    (3)點擊'寫入'開始安裝系統.安裝結束后彈出完成對話框說明安裝完成.

    PS:在SD卡安裝了Linux系統之后,再到Windows系統下查看,只剩下100多兆的容量.這是因為Linux系統下的分區Window是下是看不到的!裝了Linux系統后,SD卡會分成三種格式的分區,分別是FAT32、EXT3、SWAP2

      EXT3區屬於Linux的文件系統,就和Windows的C盤一樣;Swap區為Linux的虛擬內存區,主要在物理內存不夠用的時候做緩存用的。以上兩個,是Linux的分區。FAT32就是Windows看到的那100多兆了。

      燒錄成功后系統會因為無法識別分區而提示格式化分區,此時不要格式化,直接彈出內存卡即可。

二、啟動樹莓派

因為我沒買顯示器,所以我這里要用到 無屏幕有線遠程啟動樹莓派(SSH)

1、開啟SSH

  將SD卡使用讀卡器連接到電腦上,並打開SD卡盤符,新建“ssh”文件(無后綴)即可。

2、獲取樹莓派IP地址

  首先確認連接方式,我這里使用的連接方式為:開發用的Windows電腦通過無線網連接到Internet,而樹莓派我同過網線連在了提供無線網的路由器上。

  我這里找到3種找到樹莓派IP地址的方式:

  (1)使用遠程登陸軟件Advanced IP Scanner ,點擊‘掃描’,找到制作商為Raspberry Pi Foundation的地址信息,確認開機狀態后記住該條信息的IP。

  (2)在windows電腦上打開CMD命令行頁面,輸入arp -a可以找到樹莓派的IP地址,但是這個會出來很多IP地址,你需要從中間選出樹莓派的IP。

  (3)用手機登陸路由器的后台管理員頁面,可以看到已連接設備信息,包括IP地址。

3、使用SSH工具遠程登陸樹莓派

我這里沒有用官方推薦的Putty工具,選擇了MobaXterm這個軟件,以前學習嵌入式Linux的時候看到韋東山老師用這個軟件,發現很好用。

 

 輸入剛剛確認的樹莓派的IP地址,用戶名為pi,密碼為raspberry

PS:輸入密碼的時候是不顯示的,輸入完直接回車就OK了

  如果出現Access denied這個提示,說明你SSH沒打開,重新進行一下第二章的第一步開始SSH

出現下圖界面,說明樹莓派已經成功啟動了!!

 

 

 因為我需要用它來驅動IDS相機並做圖像處理,所以需要配置開發環境

一、安裝遠程桌面

因為用到相機,想着能看到運行情況會好很多。一般來講,通過SSH管理樹莓派已經足足夠用。

(1)在樹莓派上安裝VNCServer

  命令:sudo apt-get install tightvncserver

(2)啟動一個VNC流,啟動時會要求設置密碼。

  命令tightvncserver :1

(3)在實際使用的電腦上下載VNCViewer

  https://www.realvnc.com/en/connect/download/viewer/windows/

  安裝完成后啟動該程序,輸入樹莓派的IP地址和VNC流的代號(也就是剛剛的:1)最后輸入剛剛設置的密碼,就可以使用遠程桌面了。

二、安裝IDS相機驅動

 到IDS官網下載相應型號相機的Linux驅動.我下載到的驅動文件為.tgz格式。通過MobaXterm將驅動文件發送到樹莓派中,進入到驅動文件目錄下,解壓:sudo tar zxvf test.tgz

會得到一個.run文件,suod ./運行這個文件即可開始安裝該驅動

三、安裝python模塊

安裝任何軟件前都要使用sudo apt-get update命令更新一下軟件包。

(1)sudo pip install pyueye

(2)sudo apt-get install libopencv-dev python-opencv  安裝opencv,如果出錯,就執行一下sudo apt-get update命令

(3)sudo apt-get install python-qt4 python-qt4-doc  

四、運行示例程序進行測試

from pyueye import ueye
import numpy as np
import cv2
import sys

#---------------------------------------------------------------------------------------------------------------------------------------

#Variables
hCam = ueye.HIDS(0)             #0: first available camera;  1-254: The camera with the specified camera ID
sInfo = ueye.SENSORINFO()
cInfo = ueye.CAMINFO()
pcImageMemory = ueye.c_mem_p()
MemID = ueye.int()
rectAOI = ueye.IS_RECT()
pitch = ueye.INT()
nBitsPerPixel = ueye.INT(24)    #24: bits per pixel for color mode; take 8 bits per pixel for monochrome
channels = 3                    #3: channels for color mode(RGB); take 1 channel for monochrome
m_nColorMode = ueye.INT()       # Y8/RGB16/RGB24/REG32
bytes_per_pixel = int(nBitsPerPixel / 8)
#---------------------------------------------------------------------------------------------------------------------------------------
print("START")
print()


# Starts the driver and establishes the connection to the camera
nRet = ueye.is_InitCamera(hCam, None)
if nRet != ueye.IS_SUCCESS:
    print("is_InitCamera ERROR")

# Reads out the data hard-coded in the non-volatile camera memory and writes it to the data structure that cInfo points to
nRet = ueye.is_GetCameraInfo(hCam, cInfo)
if nRet != ueye.IS_SUCCESS:
    print("is_GetCameraInfo ERROR")

# You can query additional information about the sensor type used in the camera
nRet = ueye.is_GetSensorInfo(hCam, sInfo)
if nRet != ueye.IS_SUCCESS:
    print("is_GetSensorInfo ERROR")

nRet = ueye.is_ResetToDefault( hCam)
if nRet != ueye.IS_SUCCESS:
    print("is_ResetToDefault ERROR")

# Set display mode to DIB
nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)

# Set the right color mode
if bytes(sInfo.nColorMode.value) == ueye.IS_COLORMODE_BAYER:
    # setup the color depth to the current windows setting
    ueye.is_GetColorDepth(hCam, nBitsPerPixel, m_nColorMode)
    bytes_per_pixel = int(nBitsPerPixel / 8)
    print("IS_COLORMODE_BAYER: ", )
    print("\tm_nColorMode: \t\t", m_nColorMode)
    print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
    print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
    print()

elif bytes(sInfo.nColorMode.value) == ueye.IS_COLORMODE_CBYCRY:
    # for color camera models use RGB32 mode
    m_nColorMode = ueye.IS_CM_BGRA8_PACKED
    nBitsPerPixel = ueye.INT(32)
    bytes_per_pixel = int(nBitsPerPixel / 8)
    print("IS_COLORMODE_CBYCRY: ", )
    print("\tm_nColorMode: \t\t", m_nColorMode)
    print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
    print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
    print()

elif bytes(sInfo.nColorMode.value) == ueye.IS_COLORMODE_MONOCHROME:
    # for color camera models use RGB32 mode
    m_nColorMode = ueye.IS_CM_MONO8
    nBitsPerPixel = ueye.INT(8)
    bytes_per_pixel = int(nBitsPerPixel / 8)
    print("IS_COLORMODE_MONOCHROME: ", )
    print("\tm_nColorMode: \t\t", m_nColorMode)
    print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
    print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
    print()

else:
    # for monochrome camera models use Y8 mode
    m_nColorMode = ueye.IS_CM_MONO8
    nBitsPerPixel = ueye.INT(8)
    bytes_per_pixel = int(nBitsPerPixel / 8)
    print("else")

# Can be used to set the size and position of an "area of interest"(AOI) within an image
nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
if nRet != ueye.IS_SUCCESS:
    print("is_AOI ERROR")

width = rectAOI.s32Width
height = rectAOI.s32Height

# Prints out some information about the camera and the sensor
print("Camera model:\t\t", sInfo.strSensorName.decode('utf-8'))
print("Camera serial no.:\t", cInfo.SerNo.decode('utf-8'))
print("Maximum image width:\t", width)
print("Maximum image height:\t", height)
print()

#---------------------------------------------------------------------------------------------------------------------------------------

# Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel
nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
if nRet != ueye.IS_SUCCESS:
    print("is_AllocImageMem ERROR")
else:
    # Makes the specified image memory the active memory
    nRet = ueye.is_SetImageMem(hCam, pcImageMemory, MemID)
    if nRet != ueye.IS_SUCCESS:
        print("is_SetImageMem ERROR")
    else:
        # Set the desired color mode
        nRet = ueye.is_SetColorMode(hCam, m_nColorMode)



# Activates the camera's live video mode (free run mode)
nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
if nRet != ueye.IS_SUCCESS:
    print("is_CaptureVideo ERROR")

# Enables the queue mode for existing image memory sequences
nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
if nRet != ueye.IS_SUCCESS:
    print("is_InquireImageMem ERROR")
else:
    print("Press q to leave the programm")

#---------------------------------------------------------------------------------------------------------------------------------------

# Continuous image display
while(nRet == ueye.IS_SUCCESS):

    # In order to display the image in an OpenCV window we need to...
    # ...extract the data of our image memory
    array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)

    # bytes_per_pixel = int(nBitsPerPixel / 8)

    # ...reshape it in an numpy array...
    frame = np.reshape(array,(height.value, width.value, bytes_per_pixel))

    # ...resize the image by a half
    frame = cv2.resize(frame,(0,0),fx=0.5, fy=0.5)
    
#---------------------------------------------------------------------------------------------------------------------------------------
    #Include image data processing here

#---------------------------------------------------------------------------------------------------------------------------------------

    #...and finally display it
    cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame)

    # Press q if you want to end the loop
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
#---------------------------------------------------------------------------------------------------------------------------------------

# Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)

# Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
ueye.is_ExitCamera(hCam)

# Destroys the OpenCv windows
cv2.destroyAllWindows()

print()
print("END")

 

 結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM