gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Tue Aug 03, 2021 5:07 am

Don't change the Key.esc and Key.space entries.

When it starts the first time the lines will be 50 pixels in from each edge. When they are moved they will be stored in Hconfig.txt and loaded at startup. Delete that file and try again so you know where they will be.

Does your monitor overscan ? Is it full hd, 1920x1080 ?

Unfortunately the screen will flash as it updates the overlay. More work required. I am also looking at the possibility of using the mouse but at present the cursor doesn't show above the picamera preview.

The position of the lines should be shown at the top left eg Left: 50. Are those changing ?

HermannSW can you test the script and see if it works ?

Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Tue Aug 03, 2021 11:19 am

No there is no text changing or even showing anywhere.

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Tue Aug 03, 2021 11:29 am

Try this...

I found raspistill signal option not working, will investigate.

Also added press X to set overlay off/on, G to cycle through gridline widths.
Last edited by gordon77 on Wed Aug 04, 2021 4:30 pm, edited 1 time in total.

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Tue Aug 03, 2021 11:34 am

It should look like this at startup
Attachments
IMG_20210803_123340.jpg
IMG_20210803_123340.jpg (30.22 KiB) Viewed 953 times
Last edited by gordon77 on Tue Aug 03, 2021 3:41 pm, edited 1 time in total.

Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Tue Aug 03, 2021 11:55 am

Yes X and G are working, and looking closer; so are the other keys, lines are moving.

Sorry about the text, its been there all the time, and now I see the number changing as keys are pressed. :oops:

Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Tue Aug 03, 2021 11:56 am

gordon77 wrote:
Tue Aug 03, 2021 11:34 am
It should like this at startup
Yepp, thats what I got

Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Tue Aug 03, 2021 11:58 am

Is there a way to run the code without the Thonny compiler...Because all the key presses gets into the compiler window and mess up the code.

Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Tue Aug 03, 2021 12:12 pm

I'm now getting 12MP images on desktop after Space.
But the errors are back. Could that have something to do with me running a combination head and headless/ssh ?
You know... 2 computers, one mouse, one KB, one monitor, VGA, and HDMI via DVI :D

User avatar
B.Goode
Posts: 12825
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Crosshair and grid lines

Tue Aug 03, 2021 12:28 pm

Pilot65 wrote:
Tue Aug 03, 2021 11:58 am
Is there a way to run the code without the Thonny compiler...Because all the key presses gets into the compiler window and mess up the code.

From a shell (command line) prompt -

Code: Select all

python3 my_script_name.py



(Just for learning purposes, Python is interpreted, not compiled. And Thonny is an Editor/IDE, not a compiler or interpreter. But it will invoke the Python3 interpreter for you.)

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Tue Aug 03, 2021 3:19 pm

Pilot65 wrote:
Tue Aug 03, 2021 12:12 pm
I'm now getting 12MP images on desktop after Space.
But the errors are back. Could that have something to do with me running a combination head and headless/ssh ?
You know... 2 computers, one mouse, one KB, one monitor, VGA, and HDMI via DVI :D
Sorry l've never used headless, my best answer is the same as B.Goodes.

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Tue Aug 03, 2021 5:26 pm

I think I've improved the flickering by using an alpha layer...giving the overlay a transparent background.
Last edited by gordon77 on Wed Aug 04, 2021 4:29 pm, edited 3 times in total.

Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Tue Aug 03, 2021 5:46 pm

I saw no noticable difference...

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Tue Aug 03, 2021 6:17 pm

Pilot65 wrote:
Tue Aug 03, 2021 5:46 pm
I saw no noticable difference...
The original overlay darkened the whole image so switching it off & on, which it does to update, changed the overall brightness .
The new version doesn't darken the whole image ,
the overlay has a transparent background, so only clears and rewrites the lines and text, so l think improves it.

I have corrected a couple of errors so download it again.
Last edited by gordon77 on Wed Aug 04, 2021 6:30 am, edited 1 time in total.

Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Tue Aug 03, 2021 6:32 pm

Yes this was much better :P

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Tue Aug 03, 2021 6:33 pm

Pilot65 wrote:
Tue Aug 03, 2021 6:32 pm
Yes this was much better :P
:D

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Wed Aug 04, 2021 8:40 am

KEYS

k,l - move left gridline left/right
e,r - move right gridline left/right
y,u - move upper gridline up/down
v,b - move bottom gridline up/down
w - gridline width
c - gridline color
t - switch grid text ON/OFF
g - switch grid ON/OFF
SPACE - take photo
ESC - exit


See code below...
Last edited by gordon77 on Wed Aug 04, 2021 4:31 pm, edited 1 time in total.

Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Wed Aug 04, 2021 9:30 am

Yes...
You also see these errors?
Image278936.jpg
Image278936.jpg (138.58 KiB) Viewed 752 times

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Wed Aug 04, 2021 10:56 am

I see the errors when I exit....I use IDLE... They don't stop it working ?

Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Wed Aug 04, 2021 11:09 am

No the errors come on exit (Esc). There is something about a overlay error right at the end...
Everything is working though, it just suggests something is still not quite right.

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Wed Aug 04, 2021 11:19 am

Pilot65 wrote:
Wed Aug 04, 2021 11:09 am
No the errors come on exit (Esc). There is something about a overlay error right at the end...
Everything is working though, it just suggests something is still not quite right.

Agreed but it wasn't a priority, I'll get around to looking at it....

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Wed Aug 04, 2021 11:46 am

Is this better ? (not perfect !)

Code: Select all

import time
import datetime
import picamera
import numpy as np
from PIL import Image, ImageDraw,ImageFont
from sys import version_info as vi
import os,sys
from pynput.keyboard import Listener, Key
from pynput import keyboard
import subprocess
import signal

# set camera resolution
w = 1440
h = 1088

#initial grid settings
gridline    = 2
grid_on     = 1
gridcount   = 1
gridcolor   = [(0,0,0,255),(255,0,0,255),(0,255,0,255),(0,0,255,255),(255,255,255,255)]
lgrid       = 50
rgrid       = w - 50
ugrid       = 50
bgrid       = h - 50
show_text   = 1 
cam_preview = 1 # only for testing
e = 0

# check Hconfig.txt exists, if not then write default values
if not os.path.exists('Hconfig2.txt'):
    points = [lgrid,rgrid,ugrid,bgrid,gridline,gridcount]
    with open('Hconfig2.txt', 'w') as f:
        for item in points:
            f.write("%s\n" % item)

# read Hconfig.txt
config = []
with open("Hconfig2.txt", "r") as file:
   line = file.readline()
   while line:
      config.append(line.strip())
      line = file.readline()
config = list(map(int,config))
lgrid,rgrid,ugrid,bgrid,gridline,gridcount = config

def on_press(key):
    r = 0
    global camera,o,b,w,h,lgrid,rgrid,ugrid,bgrid,a,grid_on,gridline,gridcolor,gridcount,fnt,show_text,cam_preview,e
    # take a photo
    if key == Key.space:
        if cam_preview == 1:
            camera.stop_preview()
        camera.remove_overlay(o)
        camera.close()
        now = datetime.datetime.now()
        timestamp = now.strftime("%y%m%d%H%M%S")
        fname = str(timestamp) + '.jpg'
        cmd = "raspistill -t 500 -o " + fname
        os.system(cmd)
        camera = picamera.PiCamera()
        camera.resolution = (w, h)
        camera.framerate = 24
        if cam_preview == 1:
            camera.start_preview()
        o = camera.add_overlay(bytes(memoryview(b)),format='rgba') 
        o.alpha=128
        o.layer=3
        
    # exit
    elif key == Key.esc:
        camera.remove_overlay(o)
        camera.close()
        e = 1
        exit()
    
    elif str(key)[1:2] == 'k' and grid_on == 1:
        lgrid -= 1
        lgrid = max(lgrid,0)
        r = 1
        
    # move grid lines
    elif str(key)[1:2] == 'l' and grid_on == 1:
        lgrid += 1
        lgrid = min(lgrid,int(w/2))
        r = 1
    elif str(key)[1:2] == 'e' and grid_on == 1:
        rgrid -= 1
        rgrid = max(rgrid,int(w/2))
        r = 1
    elif str(key)[1:2] == 'r' and grid_on == 1:
        rgrid += 1
        rgrid = min(rgrid,w)
        r = 1
    elif str(key)[1:2] == 'y' and grid_on == 1:
        ugrid -= 1
        ugrid = max(ugrid,0)
        r = 1
    elif str(key)[1:2] == 'u' and grid_on == 1:
        ugrid += 1
        ugrid = min(ugrid,int(h/2))
        r = 1
    elif str(key)[1:2] == 'v' and grid_on == 1:
        bgrid -= 1
        bgrid = max(bgrid,int(h/2))
        r = 1
    elif str(key)[1:2] == 'b' and grid_on == 1:
        bgrid += 1
        bgrid = min(bgrid,h)
        r = 1
        
    # change grid line width
    elif str(key)[1:2] == 'w' and grid_on == 1:
        gridline += 1
        if gridline > 5:
            gridline = 1
        r = 1
        
    # change grid color
    elif str(key)[1:2] == 'c' and grid_on == 1:
        gridcount += 1
        if gridcount > 4:
            gridcount  = 0
        r = 1
        
    # switch text ON/OFF
    elif str(key)[1:2] == 't' and grid_on == 1:
        if show_text == 1:
            show_text = 0
        else:
            show_text = 1
        r = 1
        
    # switch grid ON/OFF
    elif str(key)[1:2] == 'g':
        if grid_on == 1:
            camera.remove_overlay(o)
            grid_on = 0
        else:
            o = camera.add_overlay(bytes(memoryview(b)),format='rgba') 
            o.alpha=128
            o.layer=3
            grid_on = 1
            
    # redraw grid   
    if r == 1:
        i = Image.fromarray(a)
        draw = ImageDraw.Draw(i)
        draw.ellipse((int(w/2) - 50, int(h/2)-50,int(w/2) + 50, int(h/2)+50), outline ='white')
        draw.line((lgrid,0,lgrid, h), fill=gridcolor[gridcount], width = gridline)
        draw.line((rgrid,0,rgrid, h), fill=gridcolor[gridcount], width = gridline)
        draw.line((0,ugrid,w,ugrid),  fill=gridcolor[gridcount], width = gridline)
        draw.line((0,bgrid,w,bgrid),  fill=gridcolor[gridcount], width = gridline)
        if show_text == 1:
            draw.text((10,10),  "Left  : " + str(lgrid),font=fnt,fill=(255,255,255,128))
            draw.text((10,60),  "Right : " + str(rgrid),font=fnt,fill=(255,255,255,128))
            draw.text((10,110), "Upper : " + str(ugrid),font=fnt,fill=(255,255,255,128))
            draw.text((10,160), "Bottom: " + str(bgrid),font=fnt,fill=(255,255,255,128))
        b = np.asarray(i)
        camera.remove_overlay(o)
        o = camera.add_overlay(bytes(memoryview(b)),format='rgba',layer=3) 
        points = [lgrid,rgrid,ugrid,bgrid,gridline,gridcount]
        with open('Hconfig2.txt', 'w') as f:
            for item in points:
                f.write("%s\n" % item)
                
# setup overlay
a = np.zeros((h, w, 4), dtype=np.uint8)
i = Image.fromarray(a)
draw = ImageDraw.Draw(i)
draw.ellipse((int(w/2) - 50, int(h/2)-50,int(w/2) + 50, int(h/2)+50), outline ='white')
draw.line((lgrid,0,lgrid, h), fill=gridcolor[gridcount], width = gridline)
draw.line((rgrid,0,rgrid, h), fill=gridcolor[gridcount], width = gridline)
draw.line((0,ugrid,w,ugrid),  fill=gridcolor[gridcount], width = gridline)
draw.line((0,bgrid,w,bgrid),  fill=gridcolor[gridcount], width = gridline)
fnt = ImageFont.truetype("FreeMono.ttf", 40)
if show_text == 1:
    draw.text((10,10),  "Left  : " + str(lgrid),font=fnt,fill=(255,255,255,128))
    draw.text((10,60),  "Right : " + str(rgrid),font=fnt,fill=(255,255,255,128))
    draw.text((10,110), "Upper : " + str(ugrid),font=fnt,fill=(255,255,255,128))
    draw.text((10,160), "Bottom: " + str(bgrid),font=fnt,fill=(255,255,255,128))
b = np.asarray(i)

# setup camera
camera = picamera.PiCamera()
camera.resolution = (w, h)
camera.framerate = 24
o = camera.add_overlay(bytes(memoryview(b)),format='rgba',layer=3) 
if cam_preview == 1:
    camera.start_preview()

# start keyboard listener
listener = keyboard.Listener(on_press=on_press)
listener.start()

while e == 0:
    pass
        


Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Wed Aug 04, 2021 12:25 pm

Yeah just sweep it under the carpet :D

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Wed Aug 04, 2021 12:37 pm

Well l think it's got all you wanted, apart from the fom for focus.

Hopefully we've saved you that $650 !!

Pilot65
Posts: 53
Joined: Sat Jul 24, 2021 10:15 pm
Location: Norway

Re: Crosshair and grid lines

Wed Aug 04, 2021 12:51 pm

Yes it is usable now. I can get this 1st manual (which I need to use this summer) scanned at 12MP.

But for the rest of the manuals it looks like I'll have to take a few steps up in HW/SW, so that 20MP camera will probably be needed later on.
Need for SPEEED, like 2-8 pictures/sec, automation :o

gordon77
Posts: 5930
Joined: Sun Aug 05, 2012 3:12 pm

Re: Crosshair and grid lines

Wed Aug 04, 2021 4:34 pm

Any chance of seeing a 12MP image, or even smaller crops you can post on here ?

Return to “Camera board”