私的AI研究会 > OpenVINO2
vi imgread.py import cv2 image_file = 'bird.jpg’ img = cv2.imread(image_file) cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
vi resize.py import cv2 image_file = 'bird.jpg' img = cv2.imread(image_file) img = cv2.resize(img, (400, 300)) cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
vi puttext.py mport cv2 image_file = 'bird.jpg’ img = cv2.imread(image_file) cv2.putText(img, "Please Don't Disturb.", (80, 530), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 255, 0), 8) cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
vi draw.py import cv2 image_file = 'bird.jpg’ img = cv2.imread(image_file) cv2.rectangle(img, (360, 240), (550, 280), (20, 20, 20), -1) cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
vi capture.py import cv2 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() cv2.imshow('image', frame) key = cv2.waitKey(1) if key != -1: break cap.release() cv2.destroyAllWindows()
import numpy as np
vi numpy.py import numpy as np a = [10, 20, 30] b = np.array(a) print(b)
python3 ndarray.py [10 20 30]変数 a はPython基礎で習ったリスト。
vi initndarray.py import numpy as np #リスト a = [10, 20, 30] print(a) #ndarray b = np.array([10, 20, 30]) print(b)
python3 initndarray.py [10, 20, 30] [10 20 30]※ リストにはカンマがついてるが、ndarrayにはカンマが無い。
vi initndarray2.py import numpy as np #リスト a = [[10, 20, 30], [40, 50, 60]] print(a) #ndarray b = np.array([[10, 20, 30], [40, 50, 60]]) print(b)
python3 initndarray2.py [[10, 20, 30], [40, 50, 60]] [[10 20 30] [40 50 60]]※ ndarrayの結果は途中で改行が入る。
vi refndarray.py import numpy as np #リスト a = [10, 20, 30] print(a[1]) #ndarray b = np.array([10, 20, 30]) print(b[1])
python3 refndarray.py 20 20
vi refndarray2.py import numpy as np #リスト a = [[10, 20, 30], [40, 50, 60]] print(a[1][2]) #ndarray b = np.array([[10, 20, 30], [40, 50, 60]]) print(b[1, 2])※ ndarrayの場合は慣例的に b[1, 2] という書き方をする。
python3 refndarray2.py 60 60
vi idxndarray.py import numpy as np a = np.array([10, 50, 40, 30, 20]) b = np.argmax(a) print(b)
python3 idxndarray.py 1配列の中の最大値は 50 だが、そのインデックス 1
shapeを使うと、形状を表示させることができる。
vi shapendarray.py import numpy as np a = np.array([1, 2, 3, 4]) print(a.shape)
python3 shapendarray.py (4,)
vi shapendarray2.py import numpy as np a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) print(a.shape)
python3 shapendarray2.py (3, 4)
vi shapendarray3.py import numpy as np a = np.array([[[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]], [[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]]) print(a.shape)
python3 shapendarray3.py (2, 3, 4)
vi type.py import numpy as np a = 100 b = 3.14 c = 'jellyfish' d = [10, 20, 30] e = np.array([10, 20, 30]) print(type(a)) print(type(b)) print(type(c)) print(type(d)) print(type(e))
python3 type.py <class 'int'> <class 'float'> <class 'str'> <class 'list'> <class 'numpy.ndarray'>
vi typeimg.py import cv2 image_file = 'bird.jpg' img = cv2.imread(image_file) print(type(img)) print(img.shape)
python3 typeimg.py <class 'numpy.ndarray'> (480, 640, 3)
vi typeimg2.py import cv2 image_file = 'cat.png' img = cv2.imread(image_file) print(img.shape[0]) print(img.shape[1])
python3 typeimg2.py 480 640
vi slice.py import numpy as np a = np.array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]) print(a[3:8])
python3 slice.py [30 40 50 60 70]
vi sliceimg.py import cv2 image_file = 'bird.jpg' img = cv2.imread(image_file) cv2.imshow('image', img[200:400, 300:600]) cv2.waitKey(0) cv2.destroyAllWindows()
vi dimension.py import numpy as np a = np.array([1, 2, 3, 4]) print(a.ndim) b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) print(b.ndim) c = np.array([[[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]], [[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]]) print(c.ndim)
python3 dimension.py 1 2 3
vi dimtrans.py import numpy as np a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) print(a) print('----------') b = a.transpose(1, 0) print(b)
python3 dimtrans.py [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] ---------- [[ 1 5 9] [ 2 6 10] [ 3 7 11] [ 4 8 12]]※ a.transpose(0, 1)と書いた場合は変化はない。2次元の場合はa.transpose(1, 0)と書くことで軸が入れ替わる。
vi transimg.py import cv2 image_file = 'bird.jpg' img = cv2.imread(image_file) cv2.imshow('image', img.transpose(1, 0, 2)) cv2.waitKey(0) cv2.destroyAllWindows()
img = img.transpose((2, 0, 1)) # HWC > CHW
vi squeeze.py import numpy as np a = np.array([1, 2, 3, 4]) print(a) print(a.ndim) print(a.shape) print('----------') b = np.array([[1, 2, 3, 4]]) print(b) print(b.ndim) print(b.shape) print('----------') c = np.array([[[1, 2, 3, 4]]]) print(c) print(c.ndim) print(c.shape)
python3 squeeze.py [1 2 3 4] 1 (4,) ---------- [[1 2 3 4]] 2 (1, 4) ---------- [[[1 2 3 4]]] 3 (1, 1, 4)変数aは1次元、bは2次元、cは3次元のndarray。
vi squeeze2.py import numpy as np a = np.array([1, 2, 3, 4]) a = np.squeeze(a) print(a) print(a.ndim) print(a.shape) print('----------') b = np.array([[1, 2, 3, 4]]) b = np.squeeze(b) print(b) print(b.ndim) print(b.shape) print('----------') c = np.array([[[1, 2, 3, 4]]]) c = np.squeeze(c) print(c) print(c.ndim) print(c.shape)
python3 squeeze2.py [1 2 3 4] 1 (4,) ---------- [1 2 3 4] 1 (4,) ---------- [1 2 3 4] 1 (4,)aは元々、大きさ1の次元はないため変化ないがbとcは余分な[]が消えている。
vi expand.py import numpy as np a = np.array([1, 2, 3, 4]) a = np.expand_dims(a, axis=0) print(a) print(a.ndim) print(a.shape) print('----------') b = np.array([[1, 2, 3, 4]]) b = np.expand_dims(b, axis=0) print(b) print(b.ndim) print(b.shape) print('----------') c = np.array([[[1, 2, 3, 4]]]) c = np.expand_dims(c, axis=0) print(c) print(c.ndim) print(c.shape)
python3 expand.py [[1 2 3 4]] 2 (1, 4) ---------- [[[1 2 3 4]]] 3 (1, 1, 4) ---------- [[[[1 2 3 4]]]] 4 (1, 1, 1, 4)大きさ1の次元が追加されている。次元数のフォーマットを合わせる際に使う場合がある。
pi@raspberrypi:~/workspace/exercise $ ls bird.jpg dimtrans.py idxndarray.py initndarray2.py refndarray.py shapendarray.py slice.py squeeze2.py typeimg.py capture.py draw.py imgread.py ndarray.py refndarray2.py shapendarray2.py sliceimg.py transimg.py typeimg2.py dimension.py expand.py initndarray.py puttext.py resize.py shapendarray3.py squeeze.py type.py