Här kommer ett litet exempel som visar hur det går att göra med OpenCV och Python. Eller rättare sagt 2, jag skrev ett litet test med ett fönster med en knapp i TKinter, men sen läste jag ditt svar att du helst ville ha levande video och kunna se vad du tog för bild så jag skrev om det lite. Det blev visserligen inte någon knapp då, (det går dock säkert att fixa till om det var ett måste) utan man får trycka mellanslag istället när man ska ta själva bilden. Det var enklare i OpenCV att läsa av tangentbordet än blanda ihop det med tkinter.
Här är fösta versionen med knapp utan live video:
Kod: Markera allt
import Tkinter as tk
import cv2
import time
# Function to take the picture and save it to a file.
def take_picture():
# Read the image for camera
ret_val, img = camera.read()
# Crop the image
cx, cy, cw, ch = 10, 10, 300, 300
cropped_img = img[cy:cy+ch, cx:cx+cw]
# Resize the cropped image, but you can use img instead if you want to resize the original.
rw, rh = 230, 80
resized_img = cv2.resize(cropped_img, (rw, rh))
# Create a filename and save the cropped and resized image
filename = time.strftime("Picture%y%m%d %H%M%S.jpg", time.localtime())
print filename + " Taken!"
cv2.imwrite(filename, resized_img)
# Create a window with a button to press when you want to take a picture.
root=tk.Tk()
root.title("Snapshoot")
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame,
text="Say Cheese !",
padx = 50,
pady = 30,
command=take_picture) # Hook on take_picture to button press.
button.pack(side=tk.LEFT)
# Create a camera object-
camera_port = 0
camera = cv2.VideoCapture(camera_port)
# Set resolution of the image you want to capture, the capured image is as close as possible
# what the hardware can deliver.
width, height = 1280, 720
camera.set(cv2.CAP_PROP_FRAME_WIDTH, width)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Call TKinter to do its job. It will call the fuction take_picture()
# every time the button is pressed.
root.mainloop()
# If you has arrived here you have pressed the close button!
print "Bye !"
och här kommer andra versionen med livevideo och mellanslag som avtryckare:
Kod: Markera allt
#import numpy as np
import cv2
import time
# Create a camera object-
camera_port = 0
camera = cv2.VideoCapture(camera_port)
# Set resolution of the image you want to capture, the capured image is as close as possible
# what the hardware can deliver.
width, height = 1280, 720
camera.set(cv2.CAP_PROP_FRAME_WIDTH, width)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
while(True):
# Capture frame-by-frame
ret, img = camera.read()
# Crop the image
cx, cy, cw, ch = 100, 190, 1080, 340
cropped_img = img[cy:cy+ch, cx:cx+cw]
# Resize the cropped image, but you can use img instead if you want to resize the original.
rw, rh = 230, 80
resized_img = cv2.resize(cropped_img, (rw, rh))
# Display the full image with the cropped area marked with a green rectangle.
img = cv2.rectangle(img, (cx, cy), (cx + cw, cy + ch), (0,255,0), 2)
cv2.imshow('Video', img)
# Check for pressed keys
# <Space> = Take snapshoot
# q or ESC quits
key = cv2.waitKey(1) & 0xFF
if key == ord('q') or key == 27:
break
if key == ord(' '):
# Create a filename and save the cropped and resized image
filename = time.strftime("Picture%y%m%d %H%M%S.jpg", time.localtime())
print filename + " Taken!"
cv2.imwrite(filename, resized_img)
# When everything done, release the capture
camera.release()
cv2.destroyAllWindows()
print "Bye!"
Det är skrivet i Python 2.7 (inte i 3an som skiljer lite. Tror det borde gå bra där med om man ändrar printsatserna så de får paranteser, typ print("Hejhej") men jag hade Pyton 2.7 installerad och det är det jag är mest van vid.
Sedan behöver du själva OpenCV som i sin tur behöver Numpy, hur du installerar det kan du hitta
>>>Här<<<