[PYTHON] Create a color sensor using a Raspberry Pi and a camera

Request: I want to see the end of processed products

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. 製品のつまり.jpg I want to know the position of the red handle of the valve (open / close display). バルブの状態.png

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.

Finished product

Connect Raspberry Pi to local WIFI. http://"ラズパイのIPアドレス"/home.php

加工品.jpg

抜き出し場所.JPG

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.

Raspberry Pi Preferences

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>

python and CSV creation

/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

Set to start python at startup.

Automatically start gazou.py with sudo. Only sudo will write the jpg image to / var / www / html / Reference site @ yuru-camper

Camera used

C270N HD WEBCAM Logitech site

What I want to do in the future

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. 設定画面.JPG

Recommended Posts

Create a color sensor using a Raspberry Pi and a camera
Create a web surveillance camera with Raspberry Pi and OpenCV
Make a simple CO2 incubator using Raspberry PI and CO2 sensor (MH-Z14A)
Create a visitor notification system using Raspberry Pi
Using a webcam with Raspberry Pi
Create a partition and then install the Raspberry Pi OS
Create your own IoT platform using raspberry pi and ESP32 (Part 3) ~ ESP32 settings Analog temperature sensor
[Raspberry Pi] Add a thermometer and a hygrometer
Create your own IoT platform using raspberry pi and ESP32 (Part 1)
Create a car meter with raspberry pi
Initial settings for using GrovePi + starter kit and camera on Raspberry Pi
Try using a QR code on a Raspberry Pi
Create a web map using Python and GDAL
Create a Mac app using py2app and Python3! !!
Raspberry Pi video camera
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Using the digital illuminance sensor TSL2561 with Raspberry Pi
Try using the temperature sensor (LM75B) on the Raspberry Pi.
Gently explain the process of making a simple serverless surveillance camera using Raspberry Pi, Gmail API and Line API
A memorandum when making a surveillance camera with Raspberry Pi
Create your own IoT platform using raspberry pi and ESP32 (Part 2) ~ ESP32 setting L Chika
Indoor monitoring using Raspberry Pi
Python beginner opens and closes interlocking camera with Raspberry Pi
Create an LCD (16x2) game with Raspberry Pi and Python
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried to automate [a certain task] using Raspberry Pi
I made a surveillance camera with my first Raspberry PI.
Shoot time-lapse from a PC camera using Python and OpenCV
Get the weather using the API and let the Raspberry Pi speak!
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
Control the motor with a motor driver using python on Raspberry Pi 3!
USB over ethernet using Raspberry pi
MQTT on Raspberry Pi and Mac
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
Create a python GUI using tkinter
Create a nested dictionary using defaultdict
Christmas classic (?) Lighting a Christmas tree with Raspberry Pi and Philips Hue
Create and deploy a Django (PTVS) app using Azure Table storage
Try using ArUco on Raspberry Pi
Create a simple scheduled batch using Docker's Python Image and parse-crontab
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Make a Kanji display compass with Raspberry Pi and Sense Hat
Create a TalkBot easily using Discord.py and A3RT's Talk API (pya3rt).
Weighing instrument using raspberry pi and hx711 (GUI display on Tkinter)
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
Create a CRUD API using FastAPI
Create a C wrapper using Boost.Python
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
Detect analog signals with A / D converter using python on Raspberry Pi 3!
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
Create a socket with an Ethernet interface (eth0, eth1) (Linux, C, Raspberry Pi)
Create a stack with a queue and a queue with a stack (from LetCode / Implement Stack using Queues, Implement Queue using Stacks)
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
I tried to create a sample to access Salesforce using Python and Bottle
Pet monitoring with Rekognition and Raspberry pi
Detect "brightness" using python on Raspberry Pi 3!
Raspberry Pi Security Infrared Camera (Python Edition)