Last active
February 21, 2023 05:06
-
-
Save davidrohweder/93174a7b795897440665bfa9c60838a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
from picamera.array import PiRGBArray | |
from picamera import PiCamera | |
import time | |
# Initialize the camera | |
camera = PiCamera() | |
camera.resolution = (640, 480) | |
camera.framerate = 30 | |
rawCapture = PiRGBArray(camera, size=(640, 480)) | |
# Allow the camera to warm up | |
time.sleep(0.1) | |
# Loop over the frames from the camera | |
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): | |
# Grab the raw NumPy array representing the image | |
image = frame.array | |
# Display the image on the screen | |
cv2.imshow("Frame", image) | |
key = cv2.waitKey(1) & 0xFF | |
# Clear the stream for the next frame | |
rawCapture.truncate(0) | |
# If the 'q' key was pressed, break from the loop | |
if key == ord("q"): | |
break | |
# Cleanup | |
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import neat | |
# Define the fitness function for the NEAT algorithm | |
def eval_genomes(genomes, config): | |
for genome_id, genome in genomes: | |
# Create a neural network from the genome | |
net = neat.nn.FeedForwardNetwork.create(genome, config) | |
# Run some example inputs through the network | |
output = net.activate((0, 0)) | |
# Evaluate the fitness of the network based on its output | |
fitness = output[0] | |
# Update the genome's fitness score | |
genome.fitness = fitness | |
# Load the NEAT configuration file | |
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, 'neat_config.txt') | |
# Create the NEAT population and evolve it for 10 generations | |
pop = neat.Population(config) | |
pop.add_reporter(neat.StdOutReporter(True)) | |
pop.add_reporter(neat.StatisticsReporter()) | |
winner = pop.run(eval_genomes, 10) | |
# Save the winning network to a file | |
with open('winner_network.txt', 'w') as f: | |
f.write(str(winner)) | |
# Load the winning network from the file and use it to make predictions | |
with open('winner_network.txt', 'r') as f: | |
genome = f.read() | |
net = neat.nn.FeedForwardNetwork.create(genome, config) | |
output = net.activate((1, 1)) | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment