Auto save without resetting stack

Discussions of Electronically Assisted Astronomy using the Live Stacking feature.
davemartin88
Posts: 46
Joined: Thu Dec 17, 2020 6:43 pm

Re: Auto save without resetting stack

#11

Post by davemartin88 »

That’s what I plan to try. Thanks!
davemartin88
Posts: 46
Joined: Thu Dec 17, 2020 6:43 pm

Re: Auto save without resetting stack

#12

Post by davemartin88 »

I’m traveling so thought I would explore ChatGPT to see what I might find useful. Used the following request to help create a script that would count livestack images and periodically save a backup image. I have no idea how to program or use python. This is what I typed-

Write a SharpCap script to capture a live stack every 10 frames and make a backup copy.

Here is the result. Look forward to trying this and making some tweaks to how files are named but this was a very interesting experiment.

Here is an IronPython SharpCap script that captures a live stack every 10 frames and makes a backup copy of each stacked image:

```python
# Import required .NET libraries
import clr
import os
from System import TimeSpan
from SharpCap.Helpers import Application
from System.IO import File, Directory

# Folder where backups will be saved
backup_folder = r"C:\SharpCap_Backups"
if not Directory.Exists(backup_folder):
Directory.CreateDirectory(backup_folder)

# Function to capture a live stack and save a backup
def capture_live_stack():
cam = Application.Camera

if cam is None:
print("No camera connected")
return

if not cam.LiveStacking.Enabled:
print("Live stacking is not enabled.")
return

# Start capturing frames
cam.StartCapture()
frame_count = 0

try:
while cam.IsCapturing:
Application.Sleep(500) # Small delay to avoid excessive CPU usage

frame_count += 1

# After every 10 frames, save the current live stack
if frame_count % 10 == 0:
live_stack_file = cam.LiveStacking.SavedFilePath
if live_stack_file and File.Exists(live_stack_file):
print(f"Live stack saved: {live_stack_file}")

# Create a backup copy
backup_file = os.path.join(backup_folder, f"Backup_{frame_count // 10}.fits")
File.Copy(live_stack_file, backup_file, True)
print(f"Backup created: {backup_file}")
else:
print("No live stack file available to save.")

except Exception as e:
print(f"Error during live stack capture: {e}")

# Stop capturing frames after completing the loop
cam.StopCapture()
print("Stopped capturing images.")

# Run the live stack capture function
capture_live_stack()
```

### Explanation:
1. **Backup folder creation**: The script first checks if a folder for backups exists (`C:\SharpCap_Backups`). If not, it creates one.
2. **Frame counting**: The script captures frames continuously, and every 10 frames, it tries to save the live stack.
3. **Live stacking check**: It ensures that live stacking is enabled before trying to access the live stack image.
4. **Backup creation**: After every 10 frames, it saves the current live stack to a backup file in the specified folder. The backup files are named `Backup_X.fits`, where `X` is the stack number (e.g., `Backup_1.fits`, `Backup_2.fits`, etc.).
5. **Loop termination**: The loop continues until capturing is manually stopped or an error occurs.

### How to use:
- Open SharpCap and make sure live stacking is enabled.
- Go to **Scripting** → **New Python Script**.
- Paste the script into the editor and run it.
- After every 10 frames in the live stack, a backup copy of the stacked image will be saved in `C:\SharpCap_Backups`.

You can modify the script to save the backup files in a different format or change the number of frames per save.
User avatar
admin
Site Admin
Posts: 17382
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Auto save without resetting stack

#13

Post by admin »

Hi,

I must admit that I wouldn't trust code that is created by ChatGPT or similar without a full read-through and taking time to understand what each part does. Looking over that script, the first error that I can see is that the loop will happen once every 500ms and will increment the frame count at that rate (regardless of what the actual exposure is), so it will try to save about every 5s. Sadly there may be more, since - particularly for SharpCap scripting - there is only a small amount of code on the web for them to be trained on, leading to a high chance of errors.

cheers,

Robin
davemartin88
Posts: 46
Joined: Thu Dec 17, 2020 6:43 pm

Re: Auto save without resetting stack

#14

Post by davemartin88 »

Thanks for the feedback. Will obviously need to test anything generated this way but feel like this at least gives me a starting point to see if I can get this functionality I’m looking for. As FYI, I let the ChatGPT know there was a loop in the code and it responded with a new version removing the automatic increase and adding a check of the count before incrementing. I won’t continue to pursue this topic on the forum but did want to say thanks for the feedback on the output from the AI.Thanks again for all the efforts you make!
Post Reply