ERROR: Local variable 'count' referenced before assignment

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
User avatar
1CM69
Posts: 63
Joined: Tue Nov 14, 2017 9:49 pm

ERROR: Local variable 'count' referenced before assignment

#1

Post by 1CM69 »

Hi,

got a small sticking point on my script with the ERROR: Local variable 'count' referenced before assignment.

Can't seem to find out what is wrong because the variable 'count' is nowhere else in my script except this one single function:

Code: Select all

def get_Capture(self):
		self.appendMessageText('Starting Capture Sequence...')
		SharpCap.ShowNotification('Starting Capture Sequence...')

		time.sleep(0.5)

		SharpCap.ShowNotification('Moving Filter Wheel To LUMINANCE Position.')
		self.appendMessageText('Moving Filter Wheel To LUMINANCE Position.')
  		SharpCap.Wheels.SelectedWheel.Position = 1
  		time.sleep(0.5)

		for count in range(1,capSeq+1):
		SharpCap.ShowNotification('Capturing LUMINANCE Channel: (' + str(count) + '/' + str(capSeq) + ')')
      		self.appendMessageText('Capturing LUMINANCE Channel: (' + str(count) + '/' + str(capSeq) + ')')
      		SharpCap.Wheels.SelectedWheel.Position = 1
      		SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = capTYPE
      		SharpCap.SelectedCamera.CaptureConfig.CaptureLimitValue = capTF
      		SharpCap.SelectedCamera.Controls.Exposure.Value = expL 
      		SharpCap.SelectedCamera.Controls.Gain.Value = gainL
      		SharpCap.SelectedCamera.PrepareToCapture()
      		SharpCap.SelectedCamera.RunCapture()
      		while True:
        		if not SharpCap.SelectedCamera.Capturing :
          			break
        	time.sleep(0.5)

      		SharpCap.ShowNotification('Capturing RED Channel: (' + str(count) + '/' + str(capSeq) + ')')
      		self.appendMessageText('Capturing RED Channel: (' + str(count) + '/' + str(capSeq) + ')')
      		SharpCap.Wheels.SelectedWheel.Position = 2
      		SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = capTYPE
      		SharpCap.SelectedCamera.CaptureConfig.CaptureLimitValue = capTF
      		SharpCap.SelectedCamera.Controls.Exposure.Value = expR 
      		SharpCap.SelectedCamera.Controls.Gain.Value = gainR
      		SharpCap.SelectedCamera.PrepareToCapture()
      		SharpCap.SelectedCamera.RunCapture()
      		while True:
        		if not SharpCap.SelectedCamera.Capturing :
          			break
        	time.sleep(0.5)

      		SharpCap.ShowNotification('Capturing GREEN Channel: (' + str(count) + '/' + str(capSeq) + ')')
      		self.appendMessageText('Capturing GREEN Channel: (' + str(count) + '/' + str(capSeq) + ')')
      		SharpCap.Wheels.SelectedWheel.Position = 3
      		SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = capTYPE
      		SharpCap.SelectedCamera.CaptureConfig.CaptureLimitValue = capTF
      		SharpCap.SelectedCamera.Controls.Exposure.Value = expG 
      		SharpCap.SelectedCamera.Controls.Gain.Value = gainG
      		SharpCap.SelectedCamera.PrepareToCapture()
      		SharpCap.SelectedCamera.RunCapture()
      		while True:
        		if not SharpCap.SelectedCamera.Capturing :
          			break
      		time.sleep(0.5)

      		SharpCap.ShowNotification('Capturing BLUE Channel: (' + str(count) + '/' + str(capSeq) + ')')
      		self.appendMessageText('Capturing BLUE Channel: (' + str(count) + '/' + str(capSeq) + ')')
      		SharpCap.Wheels.SelectedWheel.Position = 4
      		SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = capTYPE
      		SharpCap.SelectedCamera.CaptureConfig.CaptureLimitValue = capTF
      		SharpCap.SelectedCamera.Controls.Exposure.Value = expB 
      		SharpCap.SelectedCamera.Controls.Gain.Value = gainB
      		SharpCap.SelectedCamera.PrepareToCapture()
      		SharpCap.SelectedCamera.RunCapture()
      		while True:
        		if not SharpCap.SelectedCamera.Capturing :
          			break
      		time.sleep(0.5)
      
      		SharpCap.ShowNotification('Moving Filter Wheel To LUMINANCE Position.')
      		self.appendMessageText('Moving Filter Wheel To LUMINANCE Position.')
      		SharpCap.Wheels.SelectedWheel.Position = 1
      		time.sleep(0.5)
      		if count == capSeq:
        		SharpCap.ShowNotification('Capture Sequence Finished.')
		#break
Also I have commented out the last 'break' because SharpCap throws an error that the 'break' is outside the the loop but if I remove indent then it becomes 'SyntaxError: unindent does not match any outer indentation level'

Any help would be great.

Cheers..,

Kirk
User avatar
oopfan
Posts: 1320
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Re: ERROR: Local variable 'count' referenced before assignment

#2

Post by oopfan »

Kirk,

Indentation is very important in Python. It is not obvious to me nor Python what you want to do in that for-loop.

Brian
User avatar
1CM69
Posts: 63
Joined: Tue Nov 14, 2017 9:49 pm

Re: ERROR: Local variable 'count' referenced before assignment

#3

Post by 1CM69 »

oopfan wrote: Fri Aug 24, 2018 2:49 pm Kirk,

Indentation is very important in Python. It is not obvious to me nor Python what you want to do in that for-loop.

Brian
Hi Brian,

thanks for the quick reply.

Seems like the issue as you say is indentation. I copied the bulk of that code from a working testbed script & pasted it in my final script. That's where it got screwed up.

I have go back over it using Sublime Text, checking for TABS & SPACES.

It is now not throwing the errors

Code: Select all

def get_Capture(self):
		self.appendMessageText('Starting Capture Sequence...')
		SharpCap.ShowNotification('Starting Capture Sequence...')

		time.sleep(0.5)

		SharpCap.ShowNotification('Moving Filter Wheel To LUMINANCE Position.')
		self.appendMessageText('Moving Filter Wheel To LUMINANCE Position.')
		SharpCap.Wheels.SelectedWheel.Position = 1
		time.sleep(0.5)

		for count in range(1,capSeq+1):
			SharpCap.ShowNotification('Capturing LUMINANCE Channel: (' + str(count) + '/' + str(capSeq) + ')')
			self.appendMessageText('Capturing LUMINANCE Channel: (' + str(count) + '/' + str(capSeq) + ')')
			SharpCap.Wheels.SelectedWheel.Position = 1
			SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = capTYPE
			SharpCap.SelectedCamera.CaptureConfig.CaptureLimitValue = capTF
			SharpCap.SelectedCamera.Controls.Exposure.Value = expL
			SharpCap.SelectedCamera.Controls.Gain.Value = gainL
			SharpCap.SelectedCamera.PrepareToCapture()
			SharpCap.SelectedCamera.RunCapture()
			while True:
				if not SharpCap.SelectedCamera.Capturing :
					break
			time.sleep(0.5)

			SharpCap.ShowNotification('Capturing RED Channel: (' + str(count) + '/' + str(capSeq) + ')')
			self.appendMessageText('Capturing RED Channel: (' + str(count) + '/' + str(capSeq) + ')')
			SharpCap.Wheels.SelectedWheel.Position = 2
			SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = capTYPE
			SharpCap.SelectedCamera.CaptureConfig.CaptureLimitValue = capTF
			SharpCap.SelectedCamera.Controls.Exposure.Value = expR
			SharpCap.SelectedCamera.Controls.Gain.Value = gainR
			SharpCap.SelectedCamera.PrepareToCapture()
			SharpCap.SelectedCamera.RunCapture()
			while True:
				if not SharpCap.SelectedCamera.Capturing :
					break
			time.sleep(0.5)

			SharpCap.ShowNotification('Capturing GREEN Channel: (' + str(count) + '/' + str(capSeq) + ')')
			self.appendMessageText('Capturing GREEN Channel: (' + str(count) + '/' + str(capSeq) + ')')
			SharpCap.Wheels.SelectedWheel.Position = 3
			SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = capTYPE
			SharpCap.SelectedCamera.CaptureConfig.CaptureLimitValue = capTF
			SharpCap.SelectedCamera.Controls.Exposure.Value = expG
			SharpCap.SelectedCamera.Controls.Gain.Value = gainG
			SharpCap.SelectedCamera.PrepareToCapture()
			SharpCap.SelectedCamera.RunCapture()
			while True:
				if not SharpCap.SelectedCamera.Capturing :
					break
			time.sleep(0.5)

			SharpCap.ShowNotification('Capturing BLUE Channel: (' + str(count) + '/' + str(capSeq) + ')')
			self.appendMessageText('Capturing BLUE Channel: (' + str(count) + '/' + str(capSeq) + ')')
			SharpCap.Wheels.SelectedWheel.Position = 4
			SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = capTYPE
			SharpCap.SelectedCamera.CaptureConfig.CaptureLimitValue = capTF
			SharpCap.SelectedCamera.Controls.Exposure.Value = expB
			SharpCap.SelectedCamera.Controls.Gain.Value = gainB
			SharpCap.SelectedCamera.PrepareToCapture()
			SharpCap.SelectedCamera.RunCapture()
			while True:
				if not SharpCap.SelectedCamera.Capturing :
					break
			time.sleep(0.5)

			SharpCap.ShowNotification('Moving Filter Wheel To LUMINANCE Position.')
			self.appendMessageText('Moving Filter Wheel To LUMINANCE Position.')
			SharpCap.Wheels.SelectedWheel.Position = 1
			time.sleep(0.5)
			if count == capSeq:
				SharpCap.ShowNotification('Capture Sequence Finished.')
			break
but only manages to complete this part:

Code: Select all

		self.appendMessageText('Starting Capture Sequence...')
		SharpCap.ShowNotification('Starting Capture Sequence...')

		time.sleep(0.5)

		SharpCap.ShowNotification('Moving Filter Wheel To LUMINANCE Position.')
		self.appendMessageText('Moving Filter Wheel To LUMINANCE Position.')
		SharpCap.Wheels.SelectedWheel.Position = 1
		time.sleep(0.5)
and stops without proceeding any further.

The plot thickens :D

Cheers..,

Kirk
User avatar
oopfan
Posts: 1320
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Re: ERROR: Local variable 'count' referenced before assignment

#4

Post by oopfan »

Try printing out 'capSeq'
User avatar
1CM69
Posts: 63
Joined: Tue Nov 14, 2017 9:49 pm

Re: ERROR: Local variable 'count' referenced before assignment

#5

Post by 1CM69 »

oopfan wrote: Fri Aug 24, 2018 3:22 pm Try printing out 'capSeq'
Just going through things one by one & tried as you said, it's showing a value of 0

Attempting to correct it now.

Cheers..,

Kirk
User avatar
1CM69
Posts: 63
Joined: Tue Nov 14, 2017 9:49 pm

Re: ERROR: Local variable 'count' referenced before assignment

#6

Post by 1CM69 »

Like an idiot, I forgot that the Value of NumericUpDown is Decimal.

Solved that issue now:

Code: Select all

capSeq = int(float(NumericUpDown.Value))
Cheers..,

Kirk
Post Reply