get paid

Get paid for the tasks you do online

2020年12月13日 星期日

利用 Python 下載 youtube

 最近會利用到 YouTube 上面的影片來做測試 

之前都用 web 的一些服務

但是後來發現 公司網路會阻檔這些網址

於是想到自己用 python 來 download

Python 有個很簡的 download model 叫 pytube 

直接用 pip install 就可以了

不過如果你是用舊版 3.8 前的 python , 必須要指定版本

因為 pip install 會安裝最新版的 pytube

但最新版只 support 3.8 

另外 還要用到 ssl 

因為有些網址 或 MAC 需要用到 SSL , 不然會有 error 發生

程式碼很簡單 如下

之後有空再寫成 UI 介面吧

import pytube

import ssl
ssl._create_default_https_context = ssl._create_stdlib_context
url=input("Input download youtube URL: ")
download = input("input the dir to save the file: ")
yt =pytube.YouTube(url)
# yt.download('/Users/chiusam/Downloads')
# video = youtube.streams.first()
video = yt.streams.first()
video.download('/Users/chiusam/Downloads')

2020年11月17日 星期二

Pyinstaller 打包 結果花很多時間才執行

 昨天因工作需求,  用了 pandas , OS 這幾個 library ,

因為要讓同事的 windows 跑 ,  且他的電腦沒有 python 環境 

於是直覺就用 Pyinstaller 打包 ,  原本用 -F 包單個 file , 但要快 3 分鐘才開始run 

google 一下發現是因為 -F (one file) 需要先去下載 library , 可能 pandas 這個 library 太大了

所以會等比較久

後來改成 -D 就快很多 , 但也要等快 1 秒才執行

不過以這工具來說 ok 了 


2020年11月3日 星期二

getattr 呼叫 pyqt 物件

 最今一個小專案,  會需要設定 QLineText , 但因有不少QLineText 物件

而物件每個名稱都只差最後一碼 如 line1 , line2 .... 等

如果是一個個 寫真的很笨 , code 也很長 

每次要做修改 code 會瘋掉 

當然可以用 List 把所有 QLineText 放進去, 但一個個 讀出來 

但 一多就有問題了 


如果要在同一個 class 內使用 , 前面要加 self.   

這時此時可以用 getattr ,  

先將 line{}.format{I} 取出 object 名稱

再用 getattr (self ,"line{}.format{I}).setText(i)

此時就可以變成一個物件來使用

sample code 如下 

for i in range(1, 100):
getattr(self, "ipLine{}".format(i)).setText(i)
print(getattr(self, "ipLine{}".format(i)))

2020年10月28日 星期三

Python 快速抓取美股 ticker 的方法

 最近想寫一些美股的技術分析程式, 因為跟台灣免費不太相同, 美國比較詳細得技術分析看盤或分析軟體都是要付月費的, 因為沒有即時看盤的需求, 用到的技術指標也就那幾個

於是打算自己寫個美股技術指標軟體

第一步當然就是要抓股票歷史資料摟, 不過美股代號不像台股以單存數字為主

且有不同交易所之區分,  所幸 google 到 NASDAQ FTP 有美股代號資料

下面有別人寫好的 code ,  可直接將 symbol 存成 csv


Get US Stock Symbols


2020年10月13日 星期二

python 正規化 re 的簡介

 正規化檢查是很常用的功能, 如輸入檢查 

如你是要使用者輸入如 mac address 會 IP address 時 會用來檢查輸入正不正確


import re

pattern = r"([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}"
if re.fullmatch(pattern, macaddr):
print("mac address correct")


其中 pattern 是 Mac address 格式的檢查 ,  Mac 固定式 0 ~F 12 碼 , 每兩碼要用 : 隔開

後面的定義很像文字天書 , 但等一下有快速的方法 寫出來

re.fullmatch (pattern, macaddr)  中 macaddr 是我在其他地方要求別人輸入的 Mac address 

我需要檢查使用者輸入是否正確 

如果這確 re.fullmatch 就會 return ture 

 

最後要如何快速查到你的檢查碼(pattern)呢 ?

我們可以直接用下面網址查詢別人查過的表示法即可

https://regex101.com/





2020年9月15日 星期二

Nvidia 要買下arm

今天的行為是英偉達要買下安謨
如果各國政府沒有反對的話這將是有史以來最大的一筆科技業併購案
本身因為來就用了很多arm的東西
所以將來Jason平台如    tx2 應該會有越來越多edge computer產品出現
所以學習這一塊應該可以有很長的路走

2020年8月28日 星期五

MacBook Pro 開啟 TFTP

 最近常需要更新 embeded 產品的 bootcode 會需要用到 TFTP server . 再 Windows 上可以直接下載免費的 TFTP server 軟體 , 但 MacBook Pro 找不到好用的


不過 MacBook Pro 有自帶 TFTP server , 直接用 terminal 下 command 即可啟動

但看了很多教學 , 原本已會很複雜, 但 其實很簡單, 因為我沒有打算改tftp 預設的文件目錄

sudo launchctl load -F /System/Library/LaunchDaemons/tftp.plist

sudo launchctl start com.apple.tftpd

tftp 預設的文件目錄為:
/private/tftpboot

再把要使用的檔案放在上面了路徑下即可

但如果不要是用預設就比較麻煩一點 ...


2020年6月26日 星期五

yolov3 vs SSD 辨識心得

之前的案子需要用人型偵測
原很一開始使用 SSD , 但經過兩個星期測試有時候會把較大的物體辨識成兩個
不過辨識時間相當的快
以我用 Tx2 來看可以到 30~40 frame

連假時 把辨識模組改成 yolov3 , 辨識的準確度提高了不少

但式 frame rate 只剩下不到 5 張.

不過以我的應用來看這樣子是夠用的
接來先用 yolov3 跑個一個月看看


2020年5月24日 星期日

利用 imagezmq 影像串流

imagezmq 套件 讓人輕易的可以將遠端的影像串流傳送到server 上

目前有一個專案 , 需要將 Tx2 上處理過的影像傳到 座位上以方便監控

使用 imagezmq 開發讓事情變得很間單

架構如下

Tx2 (imagezmq client) --> Server ( imagezmq server)

Client 端的 程式


#192.168.31.210 是 server (要接收串流的 IP 位置) 5555 是指定的 port .
 
sender = imagezmq.ImageSender(connect_to='tcp://192.168.31.210:5555')
 
rpi_name = socket.gethostname() # 抓取系統的名稱, 如果有多台裝置傳到 server 端 server 
才可以分辨 誰是誰

picam = VideoStream(usePiCamera=True).start()
 
time.sleep(2.0)  # allow camera sensor to warm up

while True:  # send images as stream until Ctrl-C
      image = picam.read() # 讀取 camera 的 frame
      sender.send_image(rpi_name, image) # 把影像送出



Server 端的程式

import cv2
import imagezmq
image_hub = imagezmq.ImageHub()
while True:  # show streamed images until Ctrl-C
  rpi_name, image = image_hub.recv_image()
  cv2.imshow(rpi_name, image) # 1 window for each RPi
  cv2.waitKey(1)
  image_hub.send_reply(b'OK')


有沒有很簡單

詳細內容可以參考
原作者的 git

https://github.com/jeffbass/imagezmq



2020年5月6日 星期三

Ubuntu安装virtualenvwrapper 給不同python 環境使用



隨著要寫python 專案越來越多 , 會發現 python 的版本過多是很大的困擾
這時只要用 virtualenvwrapper 給不同版本的 python 或 library 使用

下面是別人的介紹

https://www.jianshu.com/p/ccf098024eee

2020年5月1日 星期五

tx2 開機自動執行

為了將產品交給別人 demo , 但他們不會 liux command. 

所以必須要設計讓產品一上電就能動
其實這很簡單

gnome-terminal -x /home/xxxxxx. (完整的位置)

2020年4月26日 星期日

樹莓派 網路不能用 解法


我的樹莓派 網路 突然不能用
摻考網路

restart network service 就 ok 了



pi@raspberrypi4:~ $ sudo mii-tool -r eth0
restarting autonegotiation...
pi@raspberrypi4:~ $ ping 192.168.1.90
connect: Network is unreachable

Restarting the network fixed things for now.

pi@raspberrypi4:~ $ sudo service networking restart
pi@raspberrypi4:~ $ ping 192.168.1.90
PING 192.168.1.90 (192.168.1.90) 56(84) bytes of data.
64 bytes from 192.168.1.90: icmp_seq=1 ttl=64 time=0.891 ms
64 bytes from 192.168.1.90: icmp_seq=2 ttl=64 time=0.466 ms

我的樹莓派

因為一些專案所以決定用樹莓派來做比較省錢
於是直接去買現成的套組了

記得Sd cars 最好要32g 店員其實我比較喜歡用樹莓派原廠的




2020年4月12日 星期日

barcode detection and decoder -- pyzbar

前面提到如何安裝 pyzbar , 使用上也很簡單

sample code 如下

from pyzbar import pyzbar # 這邊要記住我 import 的方式

import cv2

image = cv2.imread("images.png")

barcodes = pyzbar.decode(image)


先用 cv2 讀出要辨識的 image

再用 pyzbar.decode 讀出 照片中的 barcodes 有哪些

 barcodes 內會把照片所有辨識出的 barcode , 內容與位置都會用 dict 格式純在 list

內容如下

也就是說每個 barcode , 都會有對應的 data ( qrcode 的內容)
type :(qrcode 的 type)
rect ( qrcode 的位置 x,y, w,h)
polygon 折式qrcode 四點的位置)



[
    Decoded(
        data=b'Foramenifera', type='CODE128',
        rect=Rect(left=37, top=550, width=324, height=76),
        polygon=[
            Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
            Point(x=361, y=550)
        ]
    )
    Decoded(
        data=b'Rana temporaria', type='CODE128',
        rect=Rect(left=4, top=0, width=390, height=76),
        polygon=[
            Point(x=4, y=1), Point(x=4, y=75), Point(x=394, y=76),
            Point(x=394, y=0)
        ]
    )
]

python barcode detection and decoder -- pybar 安裝

用 python 便是一些基礎的東西 越來越簡單, 原本有個專案要辨識 barcode , 本想自己硬幹

但發現有現成 performance 也很好的 Library pybar

可以做 barcode detection 與 decoder ...

但 pybar 不能直接 install , 需要現下載 zbar , windows 已經包含在 wheel 內不需再按裝, 但需要有  Visual C++ Redistributable Packages for Visual Studio 2013.

Mac 與 Linux follow 下面方式下窄即可 (出處:https://pypi.org/project/pyzbar/
Mac OS X:
brew install zbar
Linux:
sudo apt-get install libzbar0
Install this Python wrapper; use the second form to install dependencies of the command-line scripts:
pip install pyzbar

2020年2月29日 星期六

高精度感測器教學與代購

大叔最新新增加了一個部落格主要做高精度感測器教學也順便代購國外高精度感測器


https://highqualitysensors.blogspot.com/

之後也會用到python 做案例


ChartGPT 學 python 很強喔

 ChartGPT 最近很紅 ,  前一陣子有一些小 module  本來想 google  一下 語法 但發現用 chartGPT 直接請他給 sample code 反而較快 所以只要你有基本 知識 ChartGPT 可以加速你的開發 好用喔