I would like to know when the product in the automatic lane, that is, the final processed product, is completed at the machine tool factory. I want to know the position of the red handle of the valve (open / close display).
The product is yellow and the background (lane) is the primary color of white, blue, or black. The ready-made sensor is too expensive, so I want you to make it cheaply.
Connect Raspberry Pi to local WIFI. http://"ラズパイのIPアドレス"/home.php
Specify the relevant part from the camera image and extract it. Specify the color range and output GPIO when it enters the color range. The alarm lamp is activated by the output of GPIO. The alarm is canceled by resetting the alarm.
python OpenCV download Reference site: Records of experience and memos of software engineers who learned computer science
sudo apt-get install libopencv-dev
sudo apt-get install python-opencv
Apache download
Reference site @ mono_taro
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install apache2
apachectl -v
php download Reference site: The modern stone age.
sudo apt install php7.2 libapache2-mod-php7.2
# php and html creation
#### **`/var/www/html/home.php`**
```ruby
<html>
<head>
<meta http-equiv="content-type" charset="utf-8">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="refresh" content="5" >
</head>
</html>
<form action="set.php" method="get">
<body>
<h1>Camera image</h1>
<?php
$lines_reset = file('/home/pi/Documents/reset.csv');
foreach($lines_reset as $line_reset){
$data_reset = explode(',',$line_reset);
if ($data_reset[0] == 1){
echo "<FONT COLOR=\"RED\">The alarm is currently operating. To cancel it, use the lower button.</FONT>";}
}
?>
<img src="1.jpg?1">
<h2>Extracted image</h2>
<img src="2.jpg?1">
</body>
<?php
$lines = file('/home/pi/Documents/color.csv');
foreach($lines as $line){
$data = explode(',',$line);
echo '<p>';
echo 'Red R',$data[0];
echo 'Green G:',$data[1];
echo 'Blue B:',$data[2];
echo '</p>';
}
?>
<a href="./set.html">Setting screen</a>
<a href="./reset.html">Alarm reset</a>
</form>
/var/www/html/set.html
<form action="set.php" method="get">
<p>
<a href="./home.php">Return</a>
</p>
<img src="1.jpg ">
<p>Color range specification</p>
<p>Red lower limit: <input type="number" name="r_2_1" value="1" min="0" max="255"></p>
<p>Red upper limit: <input type="number" name="r_2_2" value="1" min="0" max="255"></p>
<p>Green lower limit: <input type="number" name="g_2_1" value="1" min="0" max="255"></p>
<p>Green upper limit: <input type="number" name="g_2_2" value="1" min="0" max="255"></p>
<p>Blue lower limit: <input type="number" name="b_2_1" value="1" min="0" max="255"></p>
<p>Blue upper limit: <input type="number" name="b_2_2" value="1" min="0" max="255"></p>
<p>Image extraction file: The top and bottom are the distance from the top, and the left and right are the distance from the left.</p>
<p>Top top: <input type="number" name="top" value="1" min="0" max="2000"></p>
<p>Bottom bot: <input type="number" name="bot" value="100" min="0" max="2000"></p>
<p>Left left: <input type="number" name="left" value="1" min="0" max="2000"></p>
<p>Right right: <input type="number" name="right" value="100" min="0" max="2000"></p>
<p>Shutter interval, specified number of times</p>
<p>Shutter interval seconds (s): <input type="number" name="interval" value="60" min="10" max="6000"></p>
<p>Number of times: <input type="number" name="nutime" value="1" min="1" max="100"></p>
<input type="submit" />
<p> </p>
<p>Supplementary color description</p>
<p>Red R255 G0 B0</p>
<p>Black R0 G0 B0</p>
<p>White R255 G255 B255</p>
<p>Yellow R255 G255 B0</p>
<p>Gray R128 G128 B128</p>
<p>Please search for other colors by RGB color.</p>
<p>The specified number of times is the number of times the relay operates when it is within the specified range in a row.</p>
</form>
/var/www/html/set.php
<meta http-equiv="content-type" charset="utf-8">
<?php
$r_2_1=htmlspecialchars($_GET["r_2_1"],ENT_QUOTES);
$r_2_2=htmlspecialchars($_GET["r_2_2"],ENT_QUOTES);
$g_2_1=htmlspecialchars($_GET["g_2_1"],ENT_QUOTES);
$g_2_2=htmlspecialchars($_GET["g_2_2"],ENT_QUOTES);
$b_2_1=htmlspecialchars($_GET["b_2_1"],ENT_QUOTES);
$b_2_2=htmlspecialchars($_GET["b_2_2"],ENT_QUOTES);
$top=htmlspecialchars($_GET["top"],ENT_QUOTES);
$bot=htmlspecialchars($_GET["bot"],ENT_QUOTES);
$left=htmlspecialchars($_GET["left"],ENT_QUOTES);
$right=htmlspecialchars($_GET["right"],ENT_QUOTES);
$interval=htmlspecialchars($_GET["interval"],ENT_QUOTES);
$nutime=htmlspecialchars($_GET["nutime"],ENT_QUOTES);
?>
<body>
<?php echo $_GET["r_2_1"]; ?>
<?php
$data = [
[$_GET["r_2_1"], $_GET["r_2_2"], $_GET["g_2_1"], $_GET["g_2_2"], $_GET["b_2_1"], $_GET["b_2_2"],
$_GET["top"], $_GET["bot"], $_GET["left"], $_GET["right"],
$_GET["interval"], $_GET["nutime"]],
];
$fp = fopen('/home/pi/Documents/position.csv', 'w');
foreach ($data as $line) {
fputcsv($fp, $line);
}
fclose($fp);
?>
<p>
<a href="./home.php">Return</a>
</p>
/var/www/html/reset.html
<meta http-equiv="content-type" charset="utf-8">
<meta http-equiv="Cache-Control" content="no-cache">
<form action="reset.php" method="get">
<input type="submit" />
</form>
/var/www/html/reset.php
<meta http-equiv="content-type" charset="utf-8">
<?php
$data = [["0"]];
$fp = fopen('/home/pi/Documents/reset.csv', 'w');
foreach ($data as $line) {
fputcsv($fp, $line);}
fclose($fp);
?>
<p>
<a href="./home.php">Return</a>
</p>
/home/pi/Documents/gazou.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cv2, os
import numpy as np
import csv
import time
import RPi.GPIO as GPIO
time.sleep(60)#Waiting time when starting Raspberry Pi
#GPIO settings
PIN = 14
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN, GPIO.OUT)
#Image file storage location
image_file = '/var/www/html/1.jpg' #photography
image_file_2 = '/var/www/html/2.jpg' #Verification 1
#Location of CSV for reading
poss = '/home/pi/Documents/position.csv'
colo_0 = '/home/pi/Documents/color0.csv'
colo_1 = '/home/pi/Documents/color.csv'
poss_reset = '/home/pi/Documents/reset.csv'
while True:
try :
with open(poss) as data0:#Read the cutout location
reader = csv.reader(data0)
data1 = [row for row in reader]
#Range read
r_2_1 = int(data1[0][0]) #Red lower limit img2
r_2_2 = int(data1[0][1]) #Red upper limit img2
g_2_1 = int(data1[0][2]) #Green lower limit img2
g_2_2 = int(data1[0][3]) #Green upper limit img2
b_2_1 = int(data1[0][4]) #Blue lower limit img2
b_2_2 = int(data1[0][5]) #Blue upper limit img2
#img2.Cutout part of jpg
top_2 = int(data1[0][6])
bot_2 = int(data1[0][7])
left_2 = int(data1[0][8])
right_2 = int(data1[0][9])
#Number of repetition seconds and number of operation confirmations
jobs_s = int(data1[0][10])
time.sleep(jobs_s)
with open(poss_reset) as data_reset:#Reset CSV read
reader_reset = csv.reader(data_reset)
data_reset1 = [row for row in reader_reset]
data_resset2 = int(data_reset1[0][0])
with open(colo_0) as data2:#Read the default data for writing
reader = csv.reader(data2)
data3 = [row for row in reader]
cam = cv2.VideoCapture(0)
#Read video from camera
_, img = cam.read()
#Export as an image file
img2 = img[top_2 : bot_2, left_2: right_2]
#Save image
cv2.imwrite(image_file, img)
cv2.imwrite(image_file_2, img2)
#Output RGB average value
#Make it one-dimensional with flatten and get the average with mean
r2 = img2.T[2].flatten().mean()
g2 = img2.T[1].flatten().mean()
b2 = img2.T[0].flatten().mean()
if data_resset2 == 0:
GPIO.output(PIN, GPIO.LOW) #Cut for the time being
if r_2_1 < r2 < r_2_2 and g_2_1 < g2 < g_2_2 and b_2_1 < b2 < b_2_2:
GPIO.output(PIN, GPIO.HIGH)
data_resset2 = 1
#Get RGB mean
data3[0][0] = '%.0f' % r2
data3[0][1] = '%.0f' % g2
data3[0][2] = '%.0f' % b2
#Save CSV
with open(colo_1, 'w') as f:
writer = csv.writer(f)
for data4 in data3:
writer.writerow(data4)
data_reset1[0][0] = data_resset2
with open(poss_reset, 'w') as f:
writer = csv.writer(f)
for data_reset2 in data_reset1:
writer.writerow(data_reset2)
#post process
cam.release()
cv2.destroyAllWindows()
except:
time.sleep(30)
pass
/home/pi/Documents/position.csv
1,1,1,1,1,1,1,100,1,100,60,1
/home/pi/Documents/color0.csv
100,100,100,100,100,100
/home/pi/Documents/color.csv
100,100,100,100,100,100
/home/pi/Documents/reset.csv
0
Automatically start gazou.py with sudo. Only sudo will write the jpg image to / var / www / html / Reference site @ yuru-camper
C270N HD WEBCAM Logitech site
Since the setting screen is difficult to set as shown below, I want to be able to select the extraction location with the mouse. I also want to be able to determine the color settings with the mouse. In the current state, the setting has to repeat a lot of trial and error.