顯示具有 python 標籤的文章。 顯示所有文章
顯示具有 python 標籤的文章。 顯示所有文章

2020年11月19日 星期四

opencv training images for object detection memo

官方說明網址 https://docs.opencv.org/3.4/dc/d88/tutorial_traincascade.html

OpenCV使用版本: opencv-3.4.12-vc14_vc15

#這是註解

#資料夾結構

cascade_opencv_train --- pos    #正面影像檔

                                     ---neg    #負面影像檔

#產生neg.txt正面影像檔清單, pos.txt負面影像檔清單

dir /b neg>neg.txt    #部分內容如下

pos\WIN_20201026_13_48_02_Pro.jpg

pos\WIN_20201026_13_48_16_Pro.jpg


dir /b pos>pos.txt    #部分內容如下

neg\frame_100.jpg

neg\frame_1000.jpg

neg\frame_1001.jpg


#使用內建的程式,標註之後要尋找之物件的教學圖案

opencv_annotation.exe --annotations=annotations.txt --images=pos\     #部分內容如下

pos\WIN_20201026_13_48_02_Pro.jpg 1 238 405 135 130

pos\WIN_20201026_13_48_16_Pro.jpg 1 161 346 152 153


#從標註的圖案清單建立sample.vec 檔案

opencv_createsamples -info annotations.txt -vec samples.vec        #下面是我的結果

Info file name: annotations.txt

Img file name: (NULL)

Vec file name: samples.vec

BG  file name: (NULL)

Num: 1000

BG color: 0

BG threshold: 80

Invert: FALSE

Max intensity deviation: 40

Max x angle: 1.1

Max y angle: 1.1

Max z angle: 0.5

Show samples: FALSE

Width: 24

Height: 24

Max Scale: -1

RNG Seed: 12345

Create training samples from images collection...

annotations.txt(171) : parse errorDone. Created 170 samples

#開始訓練,注意,預設的寬與高是24x24,我的正向照片為170個,負面照片為4614個。

opencv_traincascade -data . -vec samples.vec -bg neg.txt -numStages 8 -minHitRate 0.995 -maxFalseAlarmRate 0.3 -featureType LBP -numPos 170 -numNeg 4614

PARAMETERS:
cascadeDirName: .
vecFileName: samples.vec
bgFileName: neg.txt
numPos: 170
numNeg: 4614
numStages: 8
precalcValBufSize[Mb] : 1024
precalcIdxBufSize[Mb] : 1024
acceptanceRatioBreakValue : -1
stageType: BOOST
featureType: LBP
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.3
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
Number of unique features given windowSize [24,24] : 8464

===== TRAINING 0-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 1
Precalculation time: 0.914
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0247074|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 1 seconds.

===== TRAINING 1-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 0.024649
Precalculation time: 1.021
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0264413|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 3 seconds.

===== TRAINING 2-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 0.0021845
Precalculation time: 0.979
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0383615|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 26 seconds.

===== TRAINING 3-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    4614 : 0.00032392
Precalculation time: 0.91
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|0.0448635|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 59 seconds.

===== TRAINING 4-stage =====
<BEGIN
POS count : consumed   170 : 170
NEG count : acceptanceRatio    0 : 0
Required leaf false alarm rate achieved. Branch training terminated.

#訓練之後就會產生cascade.xml的檔案,可以從下面的連結1下載demo程式去偵測看看。

#最後

#參考網頁,一開始建議先從基本的開始做,之後再看1.的連結,會比較懂。別像我花了一堆時間train,結果都不理想。後來才從2.的連結與官網才成功。

1. https://chtseng.wordpress.com/2020/02/07/%E5%A6%82%E4%BD%95%E8%A8%93%E7%B7%B4%E4%B8%80%E5%80%8B%E8%B2%93%E8%87%89%E5%81%B5%E6%B8%AC%E5%99%A8/

2. https://johnallen.github.io/opencv-object-detection-tutorial/



2017年12月22日 星期五

Python networking note

Function Not ready, don't use it. 
netstat -aon

#Ethan_s.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import socket, os, sys, time, subprocess        # Import socket module
from subprocess import Popen, PIPE
reload(sys)
sys.setdefaultencoding('utf8')
#0==================================                                #http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php
port = 60001                    # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
s.bind((host, port))            # Bind to the port
s.listen(5)                     # Now wait for client connection.
print 'Server listening....'

#1==================================
while True: #Loop for listening
    conn, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print('Server received', conn.recv(1024)) #R0 Hello server!, repr() like as str()

#2==================================
    try:
        while conn:
            data = conn.recv(4096)  #R1, 4096 is recommand
            print ("Receiving...: "+data)
           
            p=subprocess.Popen(data,shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
            stdout,stderr =p.communicate()         

            send_data = str("\nServer got msg: \n===========START================\n"+
                        "cmd: "+data+"\n"+
                        "stdout: \n"+stdout+"\n"+
                        "stderr: \n"+stderr+"\n"+
                        "\n===========END================\n")
           

            conn.sendall(send_data) #S1, buffer不足
            #while (send_data):
            #    conn.send(send_data)
            print("Sending... "+send_data)


    except IOError as e:
        print "I/O error({0}): {1}".format(e.errno, e.strerror)
    except socket.error as msg:
        print "Socket Error: %s" % msg
    except TypeError as msg:
        print "Type Error: %s" % msg
    finally:
        print('Successfully !')
        #print('Sending finished')
        #conn.send('Thank you for connecting\n')
        conn.close()
        print '\n\n\nServer listening....'



"""
Know issue buffer not ready 如果傳太多data會lose data
"""




#Ethan_c.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import socket, os, sys, time          # Import socket module
from subprocess import Popen   #http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php
reload(sys)
sys.setdefaultencoding('utf8')
#0==================================
port = 60001   
s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
                                # Reserve a port for your service.



#1==================================
s.connect(('10.110.145.250', port))    #s.connect((host, port))

#10.110.146.77
#10.110.136.52
#10.110.136.159

s.send("Hello server!") #S0


#2==================================
try:
    while True :
        remote_cmd = raw_input("please input the cmd (default is dir): \n")#"please input the cmd (default is dir):", "dir"
     
        if remote_cmd.lower() == "":
            recv_data = s.recv(1024)
            print ("\nReceive... \n"+ recv_data)     
        elif remote_cmd.lower() == "exit" or remote_cmd.lower() == "q":
            s.send("Close by client")
            s.close()
        else:
            s.send(remote_cmd)   #S1
            print('Sending... ',repr(remote_cmd))

        time.sleep(1)   #delay
        recv_data = s.recv(1024)    #R1, buffer not enough
        if recv_data:
            print ("\nReceive... \n"+ recv_data)
            recv_data = s.recv(1024)  # keep waiting for data coming. Bug at here
            if not recv_data:
                s.close()
                break
            time.sleep(1)





except IOError as e:
    print "I/O error({0}): {1}".format(e.errno, e.strerror)
except socket.error as msg:
    print "Socket Error: %s" % msg
except TypeError as msg:
    print "Type Error: %s" % msg 
finally:
    #print("Sever: " +s.recv(1024))
    s.close()
    print('connection closed')

#p=Popen('received_file.bat')
#stdout,stderr =p.communicate()

"""
Know issue buffer not ready 如果傳太多data會lose data
"""

2017年9月22日 星期五

Python Note



==========py to exe with sign==========
參考網址https://steward-fu.github.io/website/driver/wdm/self_sign.htm
或這個也寫得不錯http://mqjing.blogspot.tw/2009/03/plugin-activex-cab-signtool.html

signtool get from MS SDK or Visual C++ for Python (http://aka.ms/vcpython27)

First time
Makecert -r -pe -ss YourName YourName.cer
certmgr.exe -add YourName.cer -s -r localMachine root
After
signtool sign /s YourName YourApp.exe

==========bundle exe to py.exe==========
http://blog.csdn.net/Sagittarius_Warrior/article/details/73998548

currPath = sys.path[0]
os.system(currPath+"\\exe.exe") #exe.exe can not be found if without sys.path[0]
#or  add sys.path[0] to cwd path
os.path.join(os.getcwd(),sys.path[0])

C:\Python27\Scripts\pyinstaller --uac-admin ^
--uac-uiaccess ^
--win-private-assemblies ^
--clean ^
--key 1234512345 ^
-i Home.ico ^
--add-data=exe.exe;. ^
--add-data=signtool.exe;. ^
-F exe_Test.py

註: ^ 是換行的意思


====================
====================
====================
====================
======同時安裝python2 & 3==============
https://www.zhihu.com/question/21653286
py -2 v2.py
py -3 v3.py
py -2 -m pip install package
py -3 -m pip install package
header #! python2 or #! python3

==========Server & Client的部分==========
http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php

加一下自己寫的東西... https://github.com/kyc1109/devcon

# Compile .py to class ==========================================
#https://stackoverflow.com/questions/14205464/converting-jython-code-into-a-java-class
Test file: A31Mod.py, A31Mod$py.class
指令如下: #若直接用Jython編譯則產出class, 但是如果直接用Python編譯則產出pyc檔。
java -jar jython-standalone-2.7.0.jar #run Jython
>>> import compileall   # import for compile multi file。
#>>> compileall.compile_dir('directory/', force=True) #compile multi file
>>> import py_compile   # import for compile single file。
>>> py_compile.compile("A31Mod.py") #compile single file from A31Mod.py to A31Mod$py.class
>>> from A31Mod import A31Mod #import A31Mod$py.class
>>> a31 = A31Mod("TC_COM_1436") # use A31Mod()
>>> a31.LAN_PING()
ping www.compal.com #result
LAN check Pass #result


Python 中文教學
http://mirror.sars.tw/Python_tut_tw/tut.html

#How to overload __init__ method based on argument type?==========================================
#https://stackoverflow.com/questions/141545/how-to-overload-init-method-based-on-argument-type
class MyData: #Quick and dirty fix
    def __init__(string=None,list=None):
        if string is not None:
            #do stuff
        elif list is not None:
            #do other stuff
        else:
            #make data empty
#===import py part 1=======================================
#Complex.py
class Complex:
    def __init__(self, realpart, imagpart): #__init__ 就是class的方法,所以等於Complex()
        self.r = realpart
        self.i = imagpart     
#test.py
from Complex import Complex
x = Complex(3.0,-4.5)
print x.r, x.i

#===import py part 2=======================================
import os, subprocess, time
#變數和呼叫的方法要前面要加self.,所以會有一堆self.xxx。
class DevCompareMod:
    def __init__(self, original_file, unoriginal_file, log_file): #__init__ equal DevCompareMod()
        self.original_file = original_file #"original.txt"
        self.unoriginal_file = unoriginal_file #"unoriginal.txt"
        self.log_file = log_file #"log_DevCompare.txt"
    def wLog(self, txtLog):
        if not os.path.exists(self.log_file):
            wLog=open(self.log_file, "w")
            wLog.write(txtLog)
            wLog.close()     
        else:
            wLog=open(self.log_file, "a")
            wLog.write(txtLog)
            wLog.close()
    self.wLog(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+"=======================================\n")
    self.wLog(strLost)

#使用方式
from DevCompareMod import DevCompareMod
DevCompareMod("original.txt","unoriginal.txt","log_DevCompare.txt")

#===import py part 3=======================================
#__init__ 和 __name__是可以一起用的
class DevCompareMod:
    def __init__(self): #__init__ equal DevCompareMod()

if __name__ == '__main__':
if len(sys.argv) < 2 or sys.argv[1].lower()=="/h" or sys.argv[1].lower()=="-h" or sys.argv[1].lower()=="/?" or sys.argv[1].lower()=="-?" : #len小於2也就是不帶參數啦
print "no/wrong argument."
else:
DevCompareMod()
# 參考網址 http://www.kaiching.org/2012/12/Python-Constructor.html




http://usyiyi.cn/documents/python_278/library/subprocess.html
簡中的翻譯版

建議優先使用 subprocess.call()或是subprocess.check_call()或是subprocess.check_output()(回傳值不為0則丟出例外),
如果不行的話,再用Popen 搭配communicate()
#Popen VS. check_output
#因為check_outputt因為已經內建stdout,所以如果有問題只會出現error code和error msg,但不會出現執行的內容。
try:
p = subprocess.check_output(["ls non_existent_file; exit 1"],shell=True, stderr=subprocess.STDOUT) #不允許stdout因為已經內建了
print p
except subprocess.CalledProcessError,e:
    print e.output
    輸出ls: cannot access 'non_existent_file': No such file or directory

q = subprocess.Popen(["ls","-a"],shell=True, stdout=PIPE, stderr=PIPE)
stdout, stderr = q.communicate() 
print stdout
輸出README.md
輸出Test_1.ipynb



https://docs.python.org/2/library/subprocess.html
#method2, output="dmesg | grep hda"
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

http://www.powenko.com/wordpress/?p=8697
翻譯的有點怪,但還可以接受啦



https://stackoverflow.com/questions/4348524/subprocess-variables
https://blog.aweimeow.tw/2016/09/09/python-subprocess-%E5%90%84%E5%87%BD%E5%BC%8F%E7%9A%84%E4%BD%BF%E7%94%A8%E6%99%82%E6%A9%9F/

參考上述網頁,Popen裡面的指令與參數,用逗號","取代空格" "可以避免不必要的錯誤。
透過PIPE把結果回傳到stdout或是stderr。


def subxx(): 
    import sys, os, subprocess               # Import socket module
    from subprocess import Popen, PIPE
    try:
        output = subprocess.Popen(["ping","localhost"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)    #useing , not space in cmd
        stdout, stderr= output.communicate()
        print ("output: \n"+str(output))
        print ("stdout: \n"+stdout)
    except subprocess.CalledProcessError:
        print('Exception handled')

subxx()

====以下為輸出結果=============================================
output: 
<subprocess.Popen object at 0x8>
stdout: 

Ping localhost [127.0.0.1] (使用 32 位元組的資料): 
回覆自 127.0.0.1: 位元組=32 time<1ms TTL=128 
回覆自 127.0.0.1: 位元組=32 time<1ms TTL=128 
回覆自 127.0.0.1: 位元組=32 time<1ms TTL=128 
回覆自 127.0.0.1: 位元組=32 time<1ms TTL=128 

127.0.0.1 的 Ping 統計資料: 
封包: 已傳送 = 4,已收到 = 4, 已遺失 = 0 (0% 遺失), 
大約的來回時間 (毫秒): 
最小值 = 0ms,最大值 = 0ms,平均 = 0ms




















2017年9月4日 星期一

Sikuli 筆記

VLC record desktop command:
vlc screen:// -I rc --screen-fps 30 :sout=#transcode{vcodec=h264}:std{access=file,dst=%homepath%/Desktop/output.mp4}
之後再打stop即可停止錄影。


1.
hover(Location(s.getBottomRight())) #終於找到了之一,S=screen, hover 可以改成click等動作
hover(Location(s.getTopLeft()).offset(100,100))    #終於找到了之二,hover offset on second monitor

#來源網址 http://www.lai18.com/content/7527573.html
#screen capture
screen = Screen()
sc = screen.capture()
shutil.move(sc.getFilename(), os.path.join(os.path.abspath('.'), "some-name.png"))

2.
跟find()有關的
Find() # FindFailed exceptions
exists() #no exceptions
Region.exists() #returns False
setFindFailedResponse(PROMPT) # PROMPT/RETRY/SKIP/ABORT/HANDLE when not found you will be prompted. 這個超好用的,在發現findfailed的時候會跳出提示,真貼心。


註:http://sikulix-2014.readthedocs.io/en/latest/region.html#exception-findfailed

3.
在Sikuli呼叫JAVA
看樣子,jar在Jython裡面被視為是一個資料夾,所以用法如方法三。

參考網站:
http://techblog.leosoto.com/jython-import-logic/
https://puremonkey2010.blogspot.tw/2014/01/jython-using-jython-to-call-java.html


下面的Jython.classJython.jar是我自己建立的測試檔案,不是官網上的Jython.jar喔!!!
#Method 1. Jython.class in *.sikuli_folder\Jython.class
import class
#Method 2. Jython.class in *.sikuli_folder\jython\Jython.class
import jython.Jython
#Method 3. Jython.jar in ..\*.sikuli_folder\sikulix\lib\Jython.jar
 import sys sys.path.append(getBundlePath()+"\\Jython.jar") #宣告jar檔案的位置 from jython import Jython #from package import class j = Jython("kyc1109","TPE") j.hello()

------Jython.jar start----------------------------------------------
package jython; /** * * @author kyc1109 */ public class Jython { private String name; private String city; /** * @param name * @param city */ public Jython(String name, String city){ this.name = name; this.city = city; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public void hello() { System.out.println("Hi this is Java by hello()"); } public static void main(String[] args) { // TODO code application logic here System.out.println("Hi this is Java"); } }

------Jython.jar end----------------------------------------------

# for Multi-Monitor
for i in range(0,Screen.getNumberScreens()): #1 = Monitor2, 0 = Monitor 1 s= Screen(i) print i bounds=s.getBounds() print bounds hover(s) #move cursor to screen 1,2...