0% found this document useful (0 votes)
9 views12 pages

Vũ Đức Tâm - 2021060552

The document outlines tasks involving video processing using Python and Arduino, including converting color videos to grayscale and binary formats, checking pixel values to control LEDs, and tracking objects with specific coordinates. It provides code snippets for both Python and Arduino to implement these functionalities. The second topic focuses on tracking an object and displaying directional text based on its x-coordinate.

Uploaded by

vuductam2712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Vũ Đức Tâm - 2021060552

The document outlines tasks involving video processing using Python and Arduino, including converting color videos to grayscale and binary formats, checking pixel values to control LEDs, and tracking objects with specific coordinates. It provides code snippets for both Python and Arduino to implement these functionalities. The second topic focuses on tracking an object and displaying directional text based on its x-coordinate.

Uploaded by

vuductam2712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HỌ VÀ TÊN:VŨ ĐỨC TÂM

MSSV : 2021060552
LỚP:DCCDTD65B

Sandra Haro
TOPIC 1
The task is to read a video from camera
(webcam) and solve issues as follow :
a, Convert color video into the gray video and
binary video ?
b, Print the matrix data of the color video ,
gray video , binary video ?
c, Check the center pixel value of the gray
video . If this value > 160 , turn ON LED at
Arduino port number 12 . If this value < 160
,turn OFF LED .
d, Check the center pixel value of the color
video . If RED color value > 160 , turn on LED
RED at Arduino port No.8 , if Green color value
> 160 ,turn on LED GREEN at Arduino No.9 , if
BLUE corlor value > 160 , turn on LED BLUE at
Arduino port [Link] cases, the LEDs
are OFF.
Code và kết quả câu a
Code và kết quả câu B
KẾT QUẢ MA TRẬN CỦA 3 VIDEO
Câu C :

Code python Code arduino


import cv2
import numpy as np
import serial
import time #print('red on, green off, blue on', val)
ser = [Link]('COM3', 9600) [Link](str(val).encode())
else:
cap = [Link](0) val = 4
if not [Link](): #print('red on, blue off, green off', val)
print('Fail to open Camera') [Link](str(val).encode())
exit() else:
while True: if value[1] > 160:
ret, frame = [Link]() if value[0] > 160:
if not ret: val = 5
print('Fail to receive frame') # print('red off, green on, blue on', val)
break [Link](str(val).encode())
width, height, _ = [Link] else:
center_x = int(width/ 2) val = 6
center_y = int(height/ 2) # print('red off, green on, blue off', val)
value = frame[center_y, center_x] [Link](str(val).encode())
print(f'value', value) else:
if value[2] > 160: if value[0] > 160:
if value[1] > 160: val = 7
if value[0] > 160: # print('red off, green off, blue on', val)
val = 1 [Link](str(val).encode())
Code python #print('led[3] on', val)
[Link](str(val).encode())
else:
val = 8
câu D : else: # print('led[3] off', val)
val = 2 [Link](str(val).encode())
#print('red on, green on, blue [Link]('video', frame)
off', val) [Link](1)
[Link](str(val).encode()) if [Link](1)& 0XFF == 27:
else: break
if value[0] > 160: [Link]()
val = 3 [Link]()
int led_red = 8;
int led_green = 9;
int led_blue = 10; else if(num == '5'){
int num ;
void setup(){
digitalWrite(led_red, LOW);
[Link](9600); digitalWrite(led_green, HIGH);
pinMode(led_red, OUTPUT); digitalWrite(led_blue, HIGH);
pinMode(led_green, OUTPUT) ; }
pinMode(led_blue, OUTPUT); else if(num == '6'){
} digitalWrite(led_red, LOW);
void loop(){
num=[Link]();
digitalWrite(led_green, HIGH);
if(num == '1') { digitalWrite(led_blue, LOW);
digitalWrite(led_red, HIGH); }
digitalWrite(led_green, HIGH); else if(num == '7'){
digitalWrite(led_blue, HIGH); digitalWrite(led_red, LOW);
} digitalWrite(led_green, LOW);
else if(num == '2'){
digitalWrite(led_red, HIGH);
digitalWrite(led_blue, HIGH);
digitalWrite(led_green, HIGH); }
digitalWrite(led_blue, LOW); else if(num == '8'){
} digitalWrite(led_red, LOW);
else if(num == '3') { digitalWrite(led_green, LOW);
digitalWrite(led_red, HIGH); digitalWrite(led_blue, LOW);
digitalWrite(led_green, LOW);
}
digitalWrite(led_blue, HIGH);
} }
else if(num== '4'){
digitalWrite(led_red, HIGH);
digitalWrite(led_green, LOW); Code arduino
}
digitalWrite(led_blue, LOW);
câu D :
Topic 2 :

The task is the tracking your object. The text


“TURN LEFT” shows on the frame when the x-
coordinate of the object is larger than 400. The
text “TURN RIGHT” shows on the frame when
the x-coordinate of the object is lower than 100.
import time if area > 100:
object_count +=1
M = [Link](contour)
import cv2 centroid_x = int(M['m10']/M['m00'])
import numpy as np centroid_y = int(M['m01']/M['m00'])
cap = [Link](0) centroid = (centroid_x, centroid_y)
lower_gr = [Link]([100,50,50]) object_ids[object_count] = centroid
upper_gr = [Link]([130, 255, 255]) (x,y,w,h) = [Link](contour)
object_count = 0 [Link](frame, (x,y), (x+w, y+h), (0, 255, 0), 2)
#[Link](frame, f'Object{object_count}', (x,y-10),
object_ids = {} cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
focal_length = 50 #pixel [Link](frame, centroid, 4, (0, 255, 0), -1)
real_width = 5 #cm if centroid_x > 400:
def calculate_distance(focal_length, real_width, [Link](frame, 'TURN LEFT', (0, 30), cv2.FONT_HERSHEY_SIMPLEX
pixel_width): 0.9, (0, 0, 255), 2)
return (focal_length * real_width) / pixel_width if centroid_x < 100:
[Link](frame, 'TURN RIGHT', (0, 30), cv2.FONT_HERSHEY_SIMPLEX
while True:
0.9, (0, 0, 255), 2)
ret, frame = [Link]() [Link](frame, 'x = ', (500, 30), cv2.FONT_HERSHEY_COMPLEX, 0.9, (
hsv = [Link](frame,cv2.COLOR_BGR2HSV) 0, 255), 2)
mask = [Link](hsv, lower_gr, upper_gr) [Link](frame, str(int(centroid_x)), (570, 30),
mask = [Link](mask, None, iterations=2) cv2.FONT_HERSHEY_COMPLEX, 0.9, (0, 0, 255), 2)
mask = [Link](mask, None, iterations=2) [Link](frame, 'y = ', (500, 70), cv2.FONT_HERSHEY_COMPLEX, 0.9, (
contours, _ = [Link]([Link](), 0, 255), 2)
[Link](frame, str(int(centroid_y)), (570, 70),
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.FONT_HERSHEY_COMPLEX, 0.9, (0, 0, 255), 2)
for contour in contours: pixel_width = w
area = [Link](contour) distance = calculate_distance(focal_length, real_width, pixel_width)
print('distance', distance)
Code python bài [Link](frame, 'distance: {:.2f} cm'.format(distance), (x, y-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.9,(0,255,0),2)
khoảng cách : [Link](0)
[Link]('Object Count', frame)
if [Link](1) == ord('c'):
break
[Link]()
[Link]()
Kết quả:
Thank
you

You might also like