Focus Assistant with Python Script

A place to report problems and bugs in SharpCap
Forum rules
‍

If you have a problem or question, please check the FAQ to see if it already has an answer : https://www.sharpcap.co.uk/sharpcap-faqs

Please also read about Troubleshooting USB Issues before posting.

*** Please do not post license keys - please report any problems with licensing to 'admin' by private message ***

Please include the following details in any bug report:

* Version of SharpCap
* Camera and other hardware being user
* Operating system version
* Contents of the SharpCap log after the problem has occurred.
[If SharpCap crashes, please send the bug report when prompted instead of including the log]
User avatar
admin
Site Admin
Posts: 17510
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Focus Assistant with Python Script

#11

Post by admin »

Hi,

no errors in that part of the log, but as soon as the focus score tool is selected, the log starts to get notifications about frames being dropped 'before transform' -

Code: Select all

Info   	17:01:01.515451	#45	Dropped frames : 128 of type DroppedBeforeTransform																		in void SharpCap.Base.CaptureProviderBase<T>.FrameDroppedBeforeTransform(FrameReference frameReference)
Looking at how quickly the number of dropped frames builds up, you are probably losing about 38 frames per second that way - since the peak frame rate of the 178 sensor is usually 40 to 50fps at max resolution, that's quite possibly the majority of the frames not being processed because the code hadn't finished processing the previous frame.

On the flip side, obviously some frames are being processed at a good rate - we are seeing logs like

Code: Select all

Debug  	17:01:17.833494	#10	SharpCap.FrameTransforms.Focus.ContrastFocusScoreTransform processed frame in 22ms										in void SharpCap.Base.CaptureProviderBase<T>.ActuallyProcessTransform(IFrameTransform frameTransform, ICapturedFrame frame, ICapturedFrame sourceFrame)
22ms isn't quite fast enough to keep up with a full 50fps, but it should let us process every other frame at the very least.

What size is the selection area when you do this? If it is very large, that will obviously take longer to process the image. If it is very small (and possibly over an all background are of the image) then no data will be created for focus score measurements - I don't think that should stop the graph from showing, but not 100% sure...

Do you see any red text in the area where the history graph should be? That was a diagnostic thing that I added to try to spot if the history graph was not being created at all - if you see the text then it gives a little info about why the history graph is not created, if you don't then the history graph *has* been created but either with no content or it is not being shown.

cheers,

Robin
StarFlea
Posts: 169
Joined: Wed Dec 21, 2022 12:22 pm

Re: Focus Assistant with Python Script

#12

Post by StarFlea »

Hi,

So, I’ve done a bit more testing, but first, to address your questions: I don’t set a specific ROI; by default, it is generated in the size shown in the screenshot.
The history window also remains completely empty; no (red) text appears:
Screenshot 2026-08-24 205802.png
Screenshot 2026-08-24 205802.png (222.8 KiB) Viewed 391 times
I have also stripped the sequence down to the absolute minimum required to trigger the effect:

Code: Select all

SEQUENCE
    RUN PYTHON FROM C:\CCD\Scripte\ReticuleNone.py
    CAMERA OPEN G3M178M
    RUN PYTHON FROM C:\CCD\Scripte\ContrastDetection.py
END SEQUENCE


Observation: The effect apparently occurs when another (arbitrary) Python script is called before the focus tool is invoked—and even then, only when the sequence is run for the first time. If the sequence is repeated within the same SharpCap instance, the graph is generated correctly.
Additionally, the graph appears immediately if the main SharpCap window is briefly minimized and then maximized.

Perhaps just a simple window redraw is needed?

cheers,
StarFlea
Attachments
ReticuleNone.py
(59 Bytes) Downloaded 1 time
ContrastDetection.py
(64 Bytes) Downloaded 1 time
User avatar
admin
Site Admin
Posts: 17510
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Focus Assistant with Python Script

#13

Post by admin »

Hi,

thanks for taking the time to work this out - I can confirm now that if I follow your steps I get the missing graph (which is good), but so far I haven't found a clean way to work around the issue... I am looking for something that has no visible side effects (so, for instance, minimize/maximize works, but would be visible). Annoyingly, if I run the software under the debugger and test this scenario, everything works fine, which makes it much harder to understand what is going on :(

I will continue to look into this.

cheers,

Robin
StarFlea
Posts: 169
Joined: Wed Dec 21, 2022 12:22 pm

Re: Focus Assistant with Python Script

#14

Post by StarFlea »

Well, I don't know which language you used to program SharpCap...

As a Java and Swing programmer, I can only speculate about the source of the error if it can't be reproduced in the debugger—though I’ve certainly encountered such cases myself.

I suspect it might be a timing issue that doesn't occur when running in the debugger. If you're working with concurrent threads, it could also be a synchronization problem.

A key characteristic is that the issue only arises if another Python script has run beforehand, and even then, only when the call is made via a sequence. Perhaps something wasn't closed or cleaned up properly in that process?
If I open the camera manually via the menu and execute the two Python scripts one after the other via the menu as well, the problem cannot be reproduced.

On the other hand, the solution might be relatively simple: could you force the graphics window to redraw after it's called via a Python script?

cheers,
StarFlea
StarFlea
Posts: 169
Joined: Wed Dec 21, 2022 12:22 pm

Re: Focus Assistant with Python Script

#15

Post by StarFlea »

The more I think about it:
this is almost certainly some stupid race condition—a dispatcher/UI thread issue...

It’s likely not just a straightforward, deterministic programming error. The debugger alters timing, thread scheduling, and sometimes even the initialization order, causing a different UI mechanism to randomly trigger a redraw.
Then the error doesn't occur! A so-called "Heisenbug"... ;)

It’s also possible that Script 1 isn't fully terminating its execution context within the sequence, or is leaving behind a thread, a COM connection, a timer, or some GUI state?

Minimizing and restoring fits this scenario, too: the data is already there, but the PAINT/GUI update chain wasn't triggered or processed correctly.
User avatar
admin
Site Admin
Posts: 17510
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Focus Assistant with Python Script

#16

Post by admin »

Hi,

yes, it's one of those quantum ones... A real pain to properly work through and find the true underlying cause :(

What I did discover was that the first python script call wasn't required to make the issue happen in my testing, and also that changing the height of the focus tool area was enough to bring the graph back. I suspect that somehow the layout of that area doesn't quite get done right in this situation, so that the plot area exists but has zero size or something silly. Any resize causes the layout to happen again and everything is fixed.

My workaround currently is to apply a 0.1 pixel margin to the right hand side of the UI control just after it is shown, then set the margin back to zero. That's enough to force the UI to re-layout after it has initially shown, but from the user's point of view, nothing will change.

This will be in the next update :)

cheers,

Robin
Post Reply