SC script in menu

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
Jean-Francois
Posts: 360
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

SC script in menu

#1

Post by Jean-Francois »

Hello Robin,

I have a good version of my script for the LED calibration of the QHY-174-GPS camera.
My plan was to generate a "startup" script for a button on the menu bar.

It is not working ... but why ?

Here the script simplified (it should work with any camera). At the end I have 4 different comment areas ... the first 3 are working good, but the last one produce an error ... it simply freeze SharpCap ... so it is needed to stop SC with the Task Manager.

If the directory "C:\Temp\" does not exist, you can define a different directory.
The script save an image (LED_test.tif) and open it in a bitmap object, then the "rectangle selection" is used and the average is calculated.
I test the "menu-script" without the image saving ... it works.
I guess that something there is blocking SharpCap.

Alternative .. use the "framehandler"/evthandler"-"CutROI" command you send me in the past ... but I never achieve to finish the code.

Is it possible to "save" the settings of the IronPython console ?
It is necessary to set all the parameters again and again.

I do some experiment with the PythonLib.zip file ... I copy some additional files in the zip and it was possible to set the file reference "inside" the zip file. My idea was to "insert" some libraries for the direct use of NumpyDotNet, IronPlot or OxyPlot.
It was some success with the xalglib.dll. It was only a partially success with the IronPlot and OxyPlot ... if only 80% of the routine is recognised, then the missing 20% generates a blocking error. May be you have an idea how to exchange IronPython with IronLab ?
I try it ... I delete PythonLib.zip file and I generate a zip file from IronLab with the name PythonLib.zip.
The script console starts and I can do some basic operation ... but not more.
(note, after all these tests I delete SharpCap and I do a clear installation)


Regards,
Jean-Francois

Code: Select all

# *******************************************************************************************
# 
# SharpCap script "Calibration_LED_menu.py"
# 2020/09/13 Jean-Francois Pittet
# Version: 1.0
#
# This script is to be placed in the list of the Startup script list:
# Menu "File", "SharpCap Settings", "Startup Scripts"
# The menu appears on the SharpCap window.
#
# The QHY-174-GPS camera hat to be connected with the LED switch-on.
# The GPS does not to be activated.
#
# In SharpCap the following actions have to be follow:
#
# - copy the script somewhere of the computer
# - start SharpCap
# - in "File" - "SharpCap Settings" - "Startup Scripts" - select this script
# - close SharpCap (the script will be loaded at the next start)
#
# - start SharpCap
# - connect QHY-174-GPS camera (I have no idea what happen if it is only the QHY-174 camera)
# - switch on the LED illumination (for the first test, no GPS is needed, the camera must be covered)
# - move the USB traffic or change the exposure time (so that SC fill the Start/End Pos values)
# - show "The Image Histogram" window
# - activate the "FX Selection Area" (for showing the red selection rectangle)
# - move and scale the selection rectangle above the most intense region of the LED illumination
# - start the script
#
# *******************************************************************************************

import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

import System
import time
import System.Drawing
import math

from array import array
from System.Windows.Forms import Application, Button, CheckBox, Form, FormStartPosition, Label, Panel, RadioButton, TextBox
from System.Drawing import Point


# (Begin) - Parameter to be changed by the user
temp_file = "c:\Temp\LED_test.tif"		# Change the directory, if necessary

# (End)   - Parameter to be changed by the user

cloneRect = SharpCap.Transforms.SelectionRect
expos_ms = SharpCap.SelectedCamera.Controls.Exposure.ExposureMs    # Need to be sure that the last value is used

# *****************************************************************************
def LED_Calibration():
    print("Dummy Calibration LED")
    SharpCap.SelectedCamera.Controls.OutputFormat.Value = 'TIFF files (*.tif)'
    SharpCap.SelectedCamera.CaptureSingleFrameTo(temp_file)
    bm = System.Drawing.Bitmap(temp_file)
    cloneBitmap = bm.Clone(cloneRect, bm.PixelFormat)
    sum = 0.0
    nb_pixel = cloneRect.Height * cloneRect.Width
    for i in range(cloneRect.Height):
        for j in range(cloneRect.Width):
            pixel = cloneBitmap.GetPixel(j,i)
            sum += float(pixel.R)
    bm.Dispose()
    cloneBitmap.Dispose()
    print cloneRect
    print sum/nb_pixel

# *****************************************************************************


class CalibrationLEDMenuForm(Form):
    def __init__(self):
        self.Text = "Calibration of the QHY-174 LED"
        self.Width = 450
        self.Height = 200
        self.setupCheckButtons()

    def setupCheckButtons(self):         
        self.Cal_LED = Button()
        self.Cal_LED.Text = 'Calibration LED'
        self.Cal_LED.Location = Point(25, 25)
        self.Cal_LED.Click += self.calib_led
        self.Cal_LED.AutoSize = True
        
        self.button_Exit = Button()
        self.button_Exit.Text = 'Exit'
        self.button_Exit.Location = Point(175, 25)
        self.button_Exit.Click += self.exit        

        self.AcceptButton = self.Cal_LED
        self.CancelButton = self.button_Exit
        self.Controls.Add(self.Cal_LED)
        self.Controls.Add(self.button_Exit)

    def calib_led(self, sender, event):
        LED_Calibration()

    def exit(self, sender, event):
        print("Stop LED clalibration script")
        self.Close()


# Try 1
#LED_Calibration()
# Try 1


# Try 2
#form = CalibrationLEDMenuForm()
#form.StartPosition = FormStartPosition.CenterScreen
#form.Show()
# Try 2


# Try 3
#def launch_form():
#    form = CalibrationLEDMenuForm()
#    form.StartPosition = FormStartPosition.CenterScreen
#    form.Show()
#
#launch_form()
# Try 3


# Try 4
#def launch_form():
#    form = CalibrationLEDMenuForm()
#    form.StartPosition = FormStartPosition.CenterScreen
#    form.Show()
#SharpCap.AddCustomButton("Cal_LED", None, "Calibration of the LED", launch_form)
# Try 4

User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: SC script in menu

#2

Post by admin »

Hi,

I tested your script and uncommented the last block and it seemed to work fine if I ran the script after opening the qhy camera. The button appeared on the toolbar and pressing it made the window appear (and work).

Putting the script in as a start-up script didn't work – I think that's because some of the earlier parts of the script assume that the camera is already open which isn't true when the script is running from start-up.

This was tested with the very latest SharpCap 3.3.

Cheers, Robin
Jean-Francois
Posts: 360
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

Re: SC script in menu

#3

Post by Jean-Francois »

Hello Robin,

With the last block uncommented ...
- the button appears and when I press it, it freezes SharpCap (I use the last 64 bit version)
- the same when I use the script in the start-up menu.

I test the last beta (64 bit) version ... yes, it works, but only manually (also no menu button).
Note, that the menu start-up does not works ... no button appears after a new start.

Concerning your script:

Code: Select all

import time

Mean = 0
Dev = 0
print Mean
print Dev

def framehandler(sender, args):
	Mean = 11
	Dev = 22
	#print Mean		# uncomment this, and it runs continuously, but why ? what is the difference to the next print Dev
	#print Dev		# uncomment this, and it runs continuously, but why ? what is the difference to the next print Dev
	if (dumpdata):
		cutout = args.Frame.CutROI(SharpCap.Transforms.SelectionRect)
		print cutout.GetStats()
		Stat = cutout.GetStats()
		print Stat.Item1
		print Stat.Item2
		Mean = Stat.Item1
		Dev = Stat.Item2
		print Mean
		print Dev
		cutout.Release()
	
		
def evthandler(sender, args):
	if (SharpCap.SelectedCamera != None):
		SharpCap.SelectedCamera.FrameCaptured -= framehandler

def monitorFrames():
	SharpCap.SelectedCamera.FrameCaptured += framehandler


dumpdata = True

SharpCap.CaptureEvent += evthandler
monitorFrames()
time.sleep(1)

dumpdata = False


print Mean
print Dev
How can I "use" the Mean and Dev outside the function framehandler ?


Regards,
Jean-Francois
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: SC script in menu

#4

Post by admin »

Hi,

these lines in your script need to be commented out for it to work as a start-up script

Code: Select all

#cloneRect = SharpCap.Transforms.SelectionRect
#expos_ms = SharpCap.SelectedCamera.Controls.Exposure.ExposureMs    # Need to be sure that the last value is used
The second one in particular will cause an error if you run a start-up script because there is no selected camera when SharpCap is starting.

Anyway, putting these values into variables when the script runs is not the right thing to do because if the values change after that point (because you move the selection or change the exposure) those variables won't get updated. The best thing to do is to ask for the value wherever you need it in the script rather than asking for it once and storing it in a variable.

To fix your other problem about getting the mean and standard deviation out of the handler, you need to use global variables - see https://www.w3schools.com/python/gloss_ ... iables.asp

I just tested the script again with the 64-bit version of SharpCap 3.3 to see if that may need difference, but I don't see any freeze up when I use the script. If SharpCap is locking up completely then after a minute or so it should offer to send a report about the freeze – please can you send this report and then tell me that you've done it, so that I can investigate further.

Cheers, Robin
Capture.PNG
Capture.PNG (391.84 KiB) Viewed 1405 times
Post Reply