# **2021/08/23 (LINE Notify)**
###### tags: `藍柏婷`
###### tags: `2021/08/23`
### **== 在 jetson nano 上裝 cv2 ==**
```p
sudo apt-get install libopencv-dev python-opencv
```
:::warning
**上面方法好像不可行**
:::
```
sudo apt update
sudo apt install python3-opencv
```
:::warning
**應該是可以了**
:::
### **== 跑程式 ==**
```
python3 LINE_Notify_4.py
```
:::warning
**#遇到問題**
```python
Traceback (most recent call last):
File "LINE_Notify_4.py", line 19, in <module>
cv2.imshow('image_win',frame) # 更新窗口“image_win”中的圖片 # 不可以刪掉,否則會沒有畫面
cv2.error: /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/highgui/src/window.cpp:304: error: (-215) size.width>0 && size.height>0 in function imshow
```
:::
```
python3 LINE_Notify_5.py
```
**#LINE_Notify_5.py**
```python=
import jetson.inference
import jetson.utils
import requests
import cv2
def send_to_line(category,pic):
params = {'message':"\n嗨,你好\n這是 \"火災警示\" 通知\n請注意!!\n偵測到 \"{0}\" 溫度過高".format(category)}
files = {'imageFile': open(pic, 'rb')}
headers = {'Authorization':'Bearer ' + "gOljwt8lLAnbpWSIXUk3RJP34Syw6NQADBtorl2GoCP"}
r = requests.post("https://notify-api.line.me/api/notify", headers=headers, params=params, files=files)
print(r.text)
net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
camera = jetson.utils.videoSource("/dev/video0") # usb camera
display = jetson.utils.videoOutput("display://0") # 'my_video.mp4' for file
# camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) # 畫面寬度設定為 1920
# camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) # 畫面高度設定為 1080
# cv2.namedWindow('image_win',flags=cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO | cv2.WINDOW_GUI_EXPANDED)
img_count = 1
while True:
img = camera.Capture()
detections = net.Detect(img)
display.Render(img)
display.SetStatus("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))
if len(detections)>=1:
# frame = camera.read()
cv2.imshow('image_win',img) # 更新窗口“image_win”中的圖片 # 不可以刪掉,否則會沒有畫面
key = cv2.waitKey(1) # 等待按鍵事件發生 等待1ms
if key == ord('q'):
break
elif key == ord('c'): # 如果c键按下,則進行圖片保存
cv2.imwrite("{}.png".format(img_count), img) # 寫入圖片 并命名圖片為 <img_count>.png
category = "microwave"
pic = '/home/iamai2021/Desktop/{}.png'.format(img_count)
send_to_line(category,pic)
img_count += 1
if not camera.IsStreaming() or not display.IsStreaming():
break
```
:::warning
**#遇到問題**
```python
Traceback (most recent call last):
File "LINE_Notify_5.py", line 4, in <module>
import cv2
ImportError: /usr/lib/aarch64-linux-gnu/libgomp.so.1: cannot allocate memory in static TLS block
```
:::
:::success
**在執行前輸入**
```
export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libgomp.so.1
```
**#參考 https://github.com/keras-team/keras-tuner/issues/317**
:::
:::warning
**#遇到問題**
```python
Traceback (most recent call last):
File "LINE_Notify_5.py", line 31, in <module>
frame = camera.read()
AttributeError: 'jetson.utils.videoSource' object has no attribute 'read'
```
:::
:::success
```python=23
while True:
img = camera.Capture()
detections = net.Detect(img)
frame=display.Render(img)
display.Render(img)
display.SetStatus("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))
if len(detections)>=1:
cv2.imwrite("{}.png".format(img_count), frame) # 寫入圖片 并命名圖片為 <img_count>.png
# frame = camera.read()
# cv2.imshow('image_win',frame) # 更新窗口“image_win”中的圖片 # 不可以刪掉,否則會沒有畫面
# key = cv2.waitKey(1) # 等待按鍵事件發生 等待1ms
# if key == ord('q'):
# break
# elif key == ord('c'): # 如果c键按下,則進行圖片保存
# cv2.imwrite("{}.png".format(img_count), frame) # 寫入圖片 并命名圖片為 <img_count>.png
category = "microwave"
pic = '/home/iamai2021/Desktop/{}.png'.format(img_count)
send_to_line(category,pic)
img_count += 1
if not camera.IsStreaming() or not display.IsStreaming():
break
```
:::
:::warning
**#遇到問題**
```python
Traceback (most recent call last):
File "LINE_Notify_5.py", line 42, in <module>
send_to_line(category,pic)
File "LINE_Notify_5.py", line 8, in send_to_line
files = {'imageFile': open(pic, 'rb')}
FileNotFoundError: [Errno 2] No such file or directory: '/home/iamai2021/Desktop/1.png'
```
**找不到圖片檔案**
**桌面上(Desktop)的檔案被存成文字檔(text.)了**
:::
:::warning
**#遇到問題**
```python
None
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
{"status":400,"message":"Invalid image."}
```
**文字檔中是空的**
:::